Skip to content

Commit

Permalink
refactor: Use input.isKey* general methods
Browse files Browse the repository at this point in the history
  • Loading branch information
warxander committed Nov 13, 2023
1 parent b22451f commit 1370f38
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Rect, Vector2 } from '../exports';
import { Input } from './input';
import { Input, InputKey } from './input';
import { Painter } from './painter';

class ItemState {
Expand Down Expand Up @@ -92,7 +92,7 @@ export class Context {
}

isItemClicked(): boolean {
return this.input.getIsLmbPressed() && this.isItemHovered();
return this.input.isKeyPressed(InputKey.LeftMouseButton) && this.isItemHovered();
}

beginItem(w: number, h: number) {
Expand Down
32 changes: 11 additions & 21 deletions src/core/input.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { Vector2 } from '../exports';

class State {
constructor(
public mousePosition = new Vector2(),
public isLmbPressed = false,
public isLmbReleased = false,
public isLmbDown = false
) {}
export enum InputKey {
LeftMouseButton = 237
}

export class Input {
private state = new State();
private mousePosition = new Vector2();

private static readonly DISABLED_CONTROLS = [
1, 2, 22, 24, 25, 36, 37, 44, 47, 53, 54, 68, 69, 70, 74, 81, 82, 83, 84, 85, 91, 92, 99, 100, 101, 102, 114,
Expand All @@ -25,27 +20,22 @@ export class Input {
if (IsPedInAnyVehicle(PlayerPedId(), false))
for (const control of Input.DISABLED_CONTROLS_IN_VEHICLE) DisableControlAction(0, control, true);

this.state = new State(
new Vector2(GetControlNormal(2, 239), GetControlNormal(2, 240)),
IsControlJustPressed(2, 237),
!this.state.isLmbPressed && IsControlJustReleased(2, 237),
!this.state.isLmbReleased && IsControlPressed(2, 237)
);
this.mousePosition = new Vector2(GetControlNormal(2, 239), GetControlNormal(2, 240));
}

getMousePosition(): Vector2 {
return this.state.mousePosition;
return this.mousePosition;
}

getIsLmbPressed(): boolean {
return this.state.isLmbPressed;
isKeyPressed(key: InputKey): boolean {
return IsControlJustPressed(2, key);
}

getIsLmbReleased(): boolean {
return this.state.isLmbReleased;
isKeyReleased(key: InputKey): boolean {
return IsControlJustReleased(2, key);
}

getIsLmbDown(): boolean {
return this.state.isLmbDown;
isKeyDown(key: InputKey): boolean {
return IsControlPressed(2, key);
}
}
45 changes: 20 additions & 25 deletions src/core/painter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Color, Image, Rect, Vector2 } from '../exports';
import { Context } from './context';
import { InputKey } from './input';
import { Style, StylePropertyValues } from './style';

class RowState {
isFirstItem = true;
size = new Vector2();
}

class WindowDragState {
position = new Vector2();
}

export enum MouseCursor {
None = 0,
Normal = 1,
Expand All @@ -36,7 +33,7 @@ export class Painter {
private textEntryIndex = -1;
private rowState: RowState | null = null;
private windowRect = new Rect();
private windowDragState: WindowDragState | null = null;
private windowDragPosition: Vector2 | null = null;
private windowSpacing = new Vector2();
private isFirstItem = true;
private itemRect = new Rect();
Expand Down Expand Up @@ -87,41 +84,39 @@ export class Painter {

private beginWindowDrag() {
const input = this.context.getInput();
const mousePosition = input.getMousePosition();

if (
!new Rect(this.position, new Vector2(this.windowRect.size.x, this.style.window.margins.y)).contains(
input.getMousePosition()
mousePosition
)
)
return;

if (!this.windowDragState) this.setMouseCursor(MouseCursor.PreGrab);

if (!input.getIsLmbPressed()) return;

if (!this.windowDragState) this.windowDragState = new WindowDragState();
if (!this.windowDragPosition) {
if (input.isKeyPressed(InputKey.LeftMouseButton)) {
this.windowDragPosition = new Vector2(mousePosition.x, mousePosition.y);
}
} else if (!input.isKeyDown(InputKey.LeftMouseButton)) {
this.windowDragPosition = null;
}

const mousePosition = input.getMousePosition();
this.windowDragState.position = new Vector2(mousePosition.x, mousePosition.y);
if (!this.windowDragPosition) this.setMouseCursor(MouseCursor.PreGrab);
}

private endWindowDrag() {
if (!this.windowDragState) return;

this.setMouseCursor(MouseCursor.Grab);
if (!this.windowDragPosition) return;

const input = this.context.getInput();
const mousePosition = this.context.getInput().getMousePosition();

if (input.getIsLmbDown()) {
const mousePosition = input.getMousePosition();
this.windowRect.position = new Vector2(
this.windowRect.position.x + mousePosition.x - this.windowDragPosition.x,
this.windowRect.position.y + mousePosition.y - this.windowDragPosition.y
);

this.windowRect.position = new Vector2(
this.windowRect.position.x + mousePosition.x - this.windowDragState.position.x,
this.windowRect.position.y + mousePosition.y - this.windowDragState.position.y
);
this.windowDragPosition = new Vector2(mousePosition.x, mousePosition.y);

this.windowDragState.position = new Vector2(mousePosition.x, mousePosition.y);
} else this.windowDragState = null;
this.setMouseCursor(MouseCursor.Grab);
}

getX(): number {
Expand Down
8 changes: 6 additions & 2 deletions src/items/slider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Rect, Vector2, context } from '../exports';
import { numberEquals } from '../core/utils';
import { Color } from '../exports';
import { InputKey } from '../core/input';

export interface ISliderResult {
isValueChanged: boolean;
Expand All @@ -21,13 +22,16 @@ export function slider(min: number, value: number, max: number, w: number): ISli
let newValue = value;

if (
(input.getIsLmbDown() || input.getIsLmbPressed()) &&
(input.isKeyDown(InputKey.LeftMouseButton) || input.isKeyPressed(InputKey.LeftMouseButton)) &&
new Rect(
new Vector2(painter.getX() - sliderStyle.tickMarkSize.x / 2, painter.getY()),
new Vector2(w + sliderStyle.tickMarkSize.x, h)
).contains(input.getMousePosition())
)
newValue = Math.min(max, Math.max(min, min + ((input.getMousePosition().x - painter.getX()) / w) * (max + min)));
newValue = Math.min(
max,
Math.max(min, min + ((input.getMousePosition().x - painter.getX()) / w) * (max + min))
);

const sh = (h - sliderStyle.height) / 2;

Expand Down

0 comments on commit 1370f38

Please sign in to comment.