Skip to content

Commit cd13979

Browse files
committed
Added sprites, changes the way projects work.
Bumped from "Pre-Alpha 1.0.0.0" to -> "Pre-Alpha 1.0.0.1".
1 parent 866471e commit cd13979

16 files changed

Lines changed: 584 additions & 117 deletions

Blah.txt

Whitespace-only changes.

Editors/EditorBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@
77

88
namespace PaintPower.Editors;
99

10+
// Base class for editors like the Paint editor or the Script editor
1011
public partial class EditorBase : UserControl
1112
{
1213
public virtual void Save() { }
1314
public virtual void Load() { }
15+
16+
public string RelativePath { get; private set; } = "";
17+
18+
public virtual void SetRelativePath(string path)
19+
{
20+
RelativePath = path;
21+
}
22+
1423
public EditorBase addText(TextBlock t) {
1524
Content = t;
1625
return this;

Editors/PaintEditor.axaml.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace PaintPower.Editors;
1414

1515
public partial class PaintEditor : EditorBase
1616
{
17-
private readonly string _relativePath;
1817
private readonly TempWorkspace _workspace;
1918

2019
private WriteableBitmap _bitmap;
@@ -35,13 +34,10 @@ private enum ToolMode { Brush, Eraser, Bucket, Hand }
3534

3635
public PaintEditor(string relativePath, TempWorkspace workspace)
3736
{
38-
_relativePath = relativePath;
3937
_workspace = workspace;
4038

4139
InitializeComponent();
4240

43-
LoadOrCreateImage();
44-
4541
HueSlider.PropertyChanged += (_, __) =>
4642
{
4743
SVPicker.Hue = HueSlider.Value;
@@ -110,23 +106,9 @@ void ResetCursor()
110106
BucketButton.Click += (_, __) => { _tool = ToolMode.Bucket; _isPanning = false; ResetCursor(); };
111107
}
112108

113-
override public void Save()
114-
{
115-
var fullPath = _workspace.MapToTemp(_relativePath);
116-
117-
using var fs = File.Open(fullPath, FileMode.Create);
118-
_bitmap.Save(fs);
119-
120-
if (!MainWindow.App.saveNeeded)
121-
{
122-
MainWindow.App.SetProjectStatus("Save Project");
123-
MainWindow.App.saveNeeded = true;
124-
}
125-
}
126-
127109
private void LoadOrCreateImage()
128110
{
129-
var fullPath = _workspace.MapToTemp(_relativePath);
111+
var fullPath = _workspace.MapToTemp(RelativePath);
130112

131113
try
132114
{
@@ -159,6 +141,26 @@ private void LoadOrCreateImage()
159141
CanvasImage.Source = _bitmap;
160142
}
161143

144+
override public void Save()
145+
{
146+
var fullPath = _workspace.MapToTemp(RelativePath);
147+
148+
using var fs = File.Open(fullPath, FileMode.Create);
149+
_bitmap.Save(fs);
150+
151+
if (!MainWindow.App.saveNeeded)
152+
{
153+
MainWindow.App.SetProjectStatus("Save Project");
154+
MainWindow.App.saveNeeded = true;
155+
}
156+
}
157+
158+
public override void SetRelativePath(string path)
159+
{
160+
base.SetRelativePath(path);
161+
LoadOrCreateImage();
162+
}
163+
162164
private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
163165
{
164166
if (_tool == ToolMode.Hand)

Editors/ScriptEditor.axaml.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace PaintPower.Editors;
1616

1717
public partial class ScriptEditor : EditorBase
1818
{
19-
private readonly string _relativePath;
2019
private readonly TempWorkspace _workspace;
2120
private TextMate.Installation _textMateInstallation;
2221
private RegistryOptions _registryOptions;
@@ -28,7 +27,9 @@ public partial class ScriptEditor : EditorBase
2827
public ScriptEditor(string relativePath, TempWorkspace workspace)
2928
{
3029
Debug.WriteLine("Loading Script Editor...");
31-
_relativePath = relativePath;
30+
//SetRelativePath(relativePath);
31+
32+
3233
_workspace = workspace;
3334

3435
AvaloniaXamlLoader.Load(this);
@@ -37,7 +38,7 @@ public ScriptEditor(string relativePath, TempWorkspace workspace)
3738

3839
if (editor != null)
3940
{
40-
editor.Text = _workspace.LoadText(relativePath);
41+
editor.Text = _workspace.LoadText(RelativePath);
4142
editor.Focus();
4243
}
4344

@@ -93,6 +94,15 @@ public ScriptEditor(string relativePath, TempWorkspace workspace)
9394
override public void Save()
9495
{
9596
var editor = this.FindControl<TextEditor>("Editor");
96-
_workspace.SaveFile(_relativePath, editor.Text);
97+
_workspace.SaveFile(RelativePath, editor.Text);
98+
}
99+
100+
public override void SetRelativePath(string path)
101+
{
102+
base.SetRelativePath(path);
103+
104+
var editor = this.FindControl<TextEditor>("Editor");
105+
if (editor != null)
106+
editor.Text = _workspace.LoadText(RelativePath);
97107
}
98108
}

FileExplorer/ExplorerView.axaml

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,30 @@
33
x:Class="PaintPower.FileExplorer.ExplorerView">
44

55
<Border Background="#99000000">
6+
<Grid RowDefinitions="Auto,Auto,*" Margin="8">
67

7-
<Grid RowDefinitions="Auto,Auto,*" Margin="8">
8+
<!-- Toolbar -->
9+
<StackPanel Orientation="Horizontal" Spacing="6">
10+
<Button Classes="white" Content="New" Click="OnNewFile"/>
11+
<Button Classes="white" Content="New Folder" Click="OnNewFolder"/>
12+
<Button Classes="white" Content="/" Click="OnGoRoot"/>
13+
<Button Classes="white" Content=".." Click="OnGoUp"/>
14+
</StackPanel>
815

9-
<!-- Toolbar -->
10-
<StackPanel Orientation="Horizontal" Spacing="6">
11-
<Button Classes="white" Content="New" Click="OnNewFile"/>
12-
<Button Classes="white" Content="New Folder" Click="OnNewFolder"/>
13-
<Button Classes="white" Content="/" Click="OnGoRoot"/>
14-
<Button Classes="white" Content=".." Click="OnGoUp"/>
15-
</StackPanel>
16+
<!-- Current Path -->
17+
<TextBlock x:Name="PathLabel"
18+
Grid.Row="1"
19+
FontSize="14"
20+
Margin="4"
21+
Foreground="White"/>
1622

17-
<!-- Current Path -->
18-
<TextBlock x:Name="PathLabel"
19-
Grid.Row="1"
20-
FontSize="14"
21-
Margin="4"
22-
Foreground="White"/>
23+
<!-- File List -->
24+
<Border Background="#20202020" Grid.Row="2">
25+
<ListBox x:Name="FileList"
26+
DoubleTapped="OnItemDoubleTapped"
27+
SelectionChanged="OnSelectionChanged"/>
28+
</Border>
2329

24-
<!-- File List -->
25-
<Border Background="#20202020" Grid.Row="2">
26-
<ListBox x:Name="FileList"
27-
Grid.Row="2"
28-
DoubleTapped="OnItemDoubleTapped"
29-
SelectionChanged="OnSelectionChanged"/>
30-
</Border>
31-
</Grid>
30+
</Grid>
3231
</Border>
3332
</UserControl>

FileExplorer/ExplorerView.axaml.cs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using PaintPower.ProjectSystem;
66
using PaintPower.Dialogs;
77
using System;
8-
using System.Diagnostics;
98
using System.Threading.Tasks;
109

1110
namespace PaintPower.FileExplorer;
@@ -14,7 +13,7 @@ public partial class ExplorerView : UserControl
1413
{
1514
public ObservableCollection<ExplorerItem> Items { get; } = new();
1615

17-
private string _currentDir;
16+
private string _currentDir = "";
1817
private TempWorkspace _workspace;
1918

2019
public ExplorerView()
@@ -26,26 +25,37 @@ public ExplorerView()
2625
public void Initialize(TempWorkspace workspace)
2726
{
2827
_workspace = workspace;
29-
_currentDir = workspace.ItemsDir;
28+
_currentDir = workspace.ActiveRoot;
3029

3130
FileList.ItemsSource = Items;
3231
Refresh();
3332
}
3433

34+
// Called when switching between project root and sprite root
35+
public void SetRoot(string newRoot)
36+
{
37+
_currentDir = newRoot;
38+
Refresh();
39+
}
40+
3541
private void Refresh()
3642
{
3743
Items.Clear();
3844

39-
Items.Clear();
4045
if (_workspace == null || !Directory.Exists(_currentDir))
4146
{
4247
PathLabel.Text = "(no project)";
4348
return;
4449
}
4550

46-
string relativePath = (_currentDir.Replace(_workspace.ItemsDir, "").Replace("\\", "/")) + "/";
51+
// Compute relative path inside ActiveRoot
52+
string relative = _currentDir.Replace(_workspace.ActiveRoot, "")
53+
.Replace("\\", "/");
54+
55+
if (string.IsNullOrEmpty(relative))
56+
relative = "/";
4757

48-
PathLabel.Text = relativePath;
58+
PathLabel.Text = relative;
4959

5060
// Folders first
5161
foreach (var dir in Directory.GetDirectories(_currentDir))
@@ -75,32 +85,26 @@ private void Refresh()
7585
// -----------------------------
7686
private void OnGoRoot(object? sender, RoutedEventArgs e)
7787
{
78-
_currentDir = _workspace.ItemsDir;
88+
_currentDir = _workspace.ActiveRoot;
7989
Refresh();
8090
}
8191

8292
private void OnGoUp(object? sender, RoutedEventArgs e)
8393
{
84-
if (_currentDir == _workspace.ItemsDir)
94+
if (_currentDir == _workspace.ActiveRoot)
8595
return;
8696

8797
_currentDir = Directory.GetParent(_currentDir)!.FullName;
8898
Refresh();
8999
}
90100

91-
// Pseudocode plan:
92-
// - The error is caused by passing 'this' (ExplorerView, a UserControl) to InputDialog.ShowAsync, which expects a Window.
93-
// - To fix, get the parent Window of this control and pass it instead.
94-
// - Use 'this.GetVisualRoot() as Window' or 'Window.GetWindow(this)' to get the parent window.
95-
// - Apply this fix in both OnNewFile and OnNewFolder.
96-
101+
// -----------------------------
102+
// Create File / Folder
103+
// -----------------------------
97104
private async void OnNewFile(object? sender, RoutedEventArgs e)
98105
{
99-
if (string.IsNullOrEmpty(_currentDir))
100-
{
101-
// Either initialize to workspace default or show error/disable UI earlier
102-
_currentDir = _workspace?.ItemsDir ?? throw new InvalidOperationException("Explorer not initialized.");
103-
}
106+
if (_workspace == null)
107+
return;
104108

105109
var dialog = new InputDialog("New File", "Enter file name:");
106110
var window = this.VisualRoot as Window;
@@ -110,11 +114,15 @@ private async void OnNewFile(object? sender, RoutedEventArgs e)
110114
return;
111115

112116
string path = Path.Combine(_currentDir, name);
113-
if (!Directory.Exists(path) && !File.Exists(path))
117+
118+
if (!File.Exists(path) && !Directory.Exists(path))
114119
File.WriteAllText(path, "");
120+
else
121+
await ShowErrorPopup();
115122

116123
MainWindow.App.SetProjectStatus("Save Project");
117124
MainWindow.App.saveNeeded = true;
125+
118126
Refresh();
119127
}
120128

@@ -130,27 +138,27 @@ private async void OnNewFolder(object? sender, RoutedEventArgs e)
130138
string path = Path.Combine(_currentDir, name);
131139

132140
if (!Directory.Exists(path) && !File.Exists(path))
133-
{
134141
Directory.CreateDirectory(path);
135-
}
136-
else {
137-
ShowErrorPopup();
138-
}
142+
else
143+
await ShowErrorPopup();
139144

140145
MainWindow.App.SetProjectStatus("Save Project");
141146
MainWindow.App.saveNeeded = true;
142147

143148
Refresh();
144149
}
145150

146-
private async Task ShowErrorPopup() {
147-
var dialog = new PopupWindowDialog("File/Folder Creation Error!", "File or folder already exists in this directory!", "Error");
151+
private async Task ShowErrorPopup()
152+
{
153+
var dialog = new PopupWindowDialog(
154+
"File/Folder Creation Error!",
155+
"File or folder already exists in this directory!",
156+
"Error"
157+
);
158+
148159
var window = this.VisualRoot as Window;
149-
try
150-
{
151-
await dialog.ShowAsync(window);
152-
}
153-
catch (Exception ex) { }
160+
try { await dialog.ShowAsync(window); }
161+
catch { }
154162
}
155163

156164
// -----------------------------
@@ -175,8 +183,6 @@ private void OnItemDoubleTapped(object? sender, RoutedEventArgs e)
175183

176184
// Open file in editor
177185
if (VisualRoot is MainWindow main)
178-
{
179186
main.OpenFile(item.FullPath);
180-
}
181187
}
182188
}

0 commit comments

Comments
 (0)