Skip to content

Commit eb1ad56

Browse files
fix pan when zoomed in
1 parent 4c40370 commit eb1ad56

File tree

2 files changed

+21
-43
lines changed

2 files changed

+21
-43
lines changed

Flamui/UiWindow.cs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ private void OnRender(double obj)
9292
if (!isInitialized)
9393
return;
9494

95+
//is this correct?
96+
9597
var start = Stopwatch.GetTimestamp();
9698

9799
using var _ = Systrace.BeginEvent(nameof(OnRender));
98100

99101
Render();
100102

101103
//ToDo cleanup
102-
Input.OnAfterFrame();
104+
103105

104106
OldHoveredElements.Clear();
105107
foreach (var uiContainer in HoveredElements)
@@ -136,13 +138,10 @@ private void OnUpdate(double obj)
136138
HandleZoomAndStuff();
137139

138140
BuildUi();
139-
}
140141

141-
public enum ZoomType
142-
{
143-
ScaleContent, //Makes the content bigger changes the layout to still fit everything
144-
ZoomContent, //Zooms in on part of the ui, while keeping everything sharp, allows pan
145-
ZoomCanvas //Zoom in on part of the image, without changing the resolution, allows pan
142+
CreateRenderInstructions();
143+
144+
Input.OnAfterFrame();
146145
}
147146

148147
private void HandleZoomAndStuff()
@@ -152,47 +151,27 @@ private void HandleZoomAndStuff()
152151
Close();
153152
}
154153

155-
if (IsKeyPressed(Key.Number1))
156-
{
157-
zoomType = ZoomType.ScaleContent;
158-
}
159-
else if (IsKeyPressed(Key.Number2))
160-
{
161-
zoomType = ZoomType.ZoomContent;
162-
}
163-
else if (IsKeyPressed(Key.Number3))
164-
{
165-
zoomType = ZoomType.ZoomCanvas;
166-
}
167-
168-
if (IsMouseButtonDown(MouseButton.Right) && zoomType is ZoomType.ZoomContent)
154+
if (IsMouseButtonDown(MouseButton.Right))
169155
{
170156
var mouseDelta = MouseDelta;
171157
ZoomTarget += mouseDelta * -1 / Zoom;
172158
}
173-
159+
174160
if (IsKeyDown(Key.ControlLeft) && ScrollDeltaY != 0)
175161
{
176162
var factor = (float)Math.Pow(1.1, ScrollDeltaY);
163+
UserScaling *= new Vector2(factor, factor);
164+
UserScaling = new Vector2(Math.Clamp(UserScaling.X, 0.1f, 10f), Math.Clamp(UserScaling.Y, 0.1f, 10f));
165+
}
177166

178-
switch (zoomType)
179-
{
180-
case ZoomType.ScaleContent:
181-
UserScaling *= new Vector2(factor, factor);
182-
UserScaling = new Vector2(Math.Clamp(UserScaling.X, 0.1f, 10f), Math.Clamp(UserScaling.Y, 0.1f, 10f));
183-
break;
184-
case ZoomType.ZoomContent:
185-
var mouseWorldPos = MousePosition;
186-
ZoomOffset = MouseScreenPosition;
187-
ZoomTarget = mouseWorldPos;
188-
Zoom *= new Vector2(factor, factor);
189-
Zoom = new Vector2(Math.Clamp(Zoom.X, 0.01f, 100f), Math.Clamp(Zoom.Y, 0.01f, 100f));
190-
break;
191-
case ZoomType.ZoomCanvas:
192-
break;
193-
default:
194-
throw new ArgumentOutOfRangeException();
195-
}
167+
if (IsKeyDown(Key.AltLeft) && ScrollDeltaY != 0)
168+
{
169+
var factor = (float)Math.Pow(1.1, ScrollDeltaY);
170+
var mouseWorldPos = MousePosition;
171+
ZoomOffset = MouseScreenPosition;
172+
ZoomTarget = mouseWorldPos;
173+
Zoom *= new Vector2(factor, factor);
174+
Zoom = new Vector2(Math.Clamp(Zoom.X, 0.01f, 100f), Math.Clamp(Zoom.Y, 0.01f, 100f));
196175
}
197176

198177
if (IsKeyPressed(Key.R))
@@ -224,7 +203,7 @@ private Matrix4X4<float> GetWorldToScreenMatrix()
224203
return origin * scale * translate;
225204
}
226205

227-
private ZoomType zoomType = ZoomType.ScaleContent;
206+
// private ZoomType zoomType = ZoomType.ScaleContent;
228207

229208
public Vector2 Zoom = new(1, 1);
230209
public Vector2 ZoomOffset = new(0, 0);
@@ -308,7 +287,6 @@ private void HitDetection()
308287

309288
private void Render()
310289
{
311-
CreateRenderInstructions();
312290
RenderToCanvas();
313291
}
314292

Sample.LayoutTest/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override void Build(Ui ui)
4747

4848
using (ui.Div().Color(C.White).ShrinkHeight())
4949
{
50-
ui.Text(loremIpsum).Color(C.Black).Multiline();
50+
ui.Text(loremIpsum).Color(C.Black).Multiline().Size(30);
5151
}
5252

5353
// var dropdown = ui.CreateDropDown(selectedOption);

0 commit comments

Comments
 (0)