Skip to content

Commit

Permalink
Merge pull request #2983 from MoritzBrueckner/debug-console-hover
Browse files Browse the repository at this point in the history
Add DebugConsole.isDebugConsoleHovered (+ node integration)
  • Loading branch information
luboslenco committed Dec 20, 2023
2 parents 78de32b + 97a99d2 commit cd2750c
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 20 deletions.
26 changes: 16 additions & 10 deletions Sources/armory/logicnode/GetDebugConsoleSettings.hx
@@ -1,5 +1,8 @@
package armory.logicnode;

#if arm_debug
import armory.trait.internal.DebugConsole;
#end

class GetDebugConsoleSettings extends LogicNode {

Expand All @@ -8,19 +11,22 @@ class GetDebugConsoleSettings extends LogicNode {
}

override function get(from: Int): Dynamic {
#if arm_debug
switch(from) {
case 0: return armory.trait.internal.DebugConsole.getVisible();
case 1: return armory.trait.internal.DebugConsole.getScale();
case 2: {
switch (armory.trait.internal.DebugConsole.getPosition()) {
case PositionStateEnum.Left: return "Left";
case PositionStateEnum.Center: return "Center";
case PositionStateEnum.Right: return "Right";
case 0: return #if arm_debug true #else false #end;
case 1: return #if arm_debug DebugConsole.getVisible() #else false #end;
case 2: return #if arm_debug DebugConsole.isDebugConsoleHovered #else false #end;
case 3: return #if arm_debug DebugConsole.getScale() #else 1.0 #end;
case 4:
#if arm_debug
switch (DebugConsole.getPosition()) {
case PositionStateEnum.Left: return "Left";
case PositionStateEnum.Center: return "Center";
case PositionStateEnum.Right: return "Right";
}
}
#else
return "";
#end
}
#end
return null;
}
}
10 changes: 10 additions & 0 deletions Sources/armory/logicnode/GetMouseStartedNode.hx
Expand Up @@ -2,8 +2,14 @@ package armory.logicnode;

import iron.system.Input;

#if arm_debug
import armory.trait.internal.DebugConsole;
#end

class GetMouseStartedNode extends LogicNode {

public var property0: Bool;

var m = Input.getMouse();
var buttonStarted: Null<String>;

Expand All @@ -14,6 +20,10 @@ class GetMouseStartedNode extends LogicNode {
override function run(from: Int) {
buttonStarted = null;

#if arm_debug
if (!property0 && DebugConsole.isDebugConsoleHovered) return;
#end

for (b in Mouse.buttons) {
if (m.started(b)) {
buttonStarted = b;
Expand Down
13 changes: 13 additions & 0 deletions Sources/armory/logicnode/MergedMouseNode.hx
@@ -1,9 +1,14 @@
package armory.logicnode;

#if arm_debug
import armory.trait.internal.DebugConsole;
#end

class MergedMouseNode extends LogicNode {

public var property0: String;
public var property1: String;
public var property2: Bool;

public function new(tree: LogicTree) {
super(tree);
Expand All @@ -12,6 +17,10 @@ class MergedMouseNode extends LogicNode {
}

function update() {
#if arm_debug
if (!property2 && DebugConsole.isDebugConsoleHovered && property0 != "moved") return;
#end

var mouse = iron.system.Input.getMouse();
var b = false;
switch (property0) {
Expand All @@ -28,6 +37,10 @@ class MergedMouseNode extends LogicNode {
}

override function get(from: Int): Dynamic {
#if arm_debug
if (!property2 && DebugConsole.isDebugConsoleHovered && property0 != "moved") return false;
#end

var mouse = iron.system.Input.getMouse();
switch (property0) {
case "started":
Expand Down
26 changes: 26 additions & 0 deletions Sources/armory/trait/internal/DebugConsole.hx
Expand Up @@ -29,6 +29,14 @@ class DebugConsole extends Trait {
public static var traceWithPosition = true;
public static var fpsAvg = 0.0;

/**
Whether any window of the debug console was hovered in the last drawn frame.
If `visible` is `false`, the value of this variable is also `false`.
**/
// NOTE If there is more than one debug console for whatever reason
// (technically possible but stupid) this will only work for the last drawn debug console
public static var isDebugConsoleHovered(default, null) = false;

static var ui: Zui;
var scaleFactor = 1.0;

Expand Down Expand Up @@ -69,6 +77,8 @@ class DebugConsole extends Trait {
var shortcutScaleIn = kha.input.KeyCode.OpenBracket;
var shortcutScaleOut = kha.input.KeyCode.CloseBracket;

var mouse: iron.system.Input.Mouse;

#if arm_shadowmap_atlas
var lightColorMap: Map<String, Int> = new Map();
var lightColorMapCount = 0;
Expand All @@ -83,6 +93,7 @@ class DebugConsole extends Trait {
keyCodeScaleOut = kha.input.KeyCode.CloseBracket) {
super();
this.scaleFactor = scaleFactor;
this.mouse = iron.system.Input.getMouse();
DebugConsole.traceWithPosition = traceWithPosition == 1;

iron.data.Data.getFont(Canvas.defaultFontName, function(font: kha.Font) {
Expand Down Expand Up @@ -233,7 +244,9 @@ class DebugConsole extends Trait {
frameTime = Scheduler.realTime() - lastTime;
lastTime = Scheduler.realTime();

isDebugConsoleHovered = false;
if (!visible) return;

var ww = Std.int(280 * scaleFactor * getScale());
// RIGHT
var wx = iron.App.w() - ww;
Expand All @@ -251,6 +264,7 @@ class DebugConsole extends Trait {
if (bindG) g.end();

ui.begin(g);

if (ui.window(hwin, wx, wy, ww, wh, true)) {

if (ui.tab(htab, "")) {}
Expand Down Expand Up @@ -908,6 +922,7 @@ class DebugConsole extends Trait {

ui.separator();
}
isDebugConsoleHovered = isDebugConsoleHovered || isZuiWindowHovered(hwin, wx, wy);

// Draw trait debug windows
var handleWinTrait = Id.handle();
Expand Down Expand Up @@ -955,6 +970,7 @@ class DebugConsole extends Trait {

ui.unindent();
}
isDebugConsoleHovered = isDebugConsoleHovered || isZuiWindowHovered(handleWindow, wx, wy);
}

ui.end(bindG);
Expand Down Expand Up @@ -990,6 +1006,16 @@ class DebugConsole extends Trait {
#end
}

function isZuiWindowHovered(hwin: zui.Zui.Handle, wx: Int, wy: Int): Bool {
var mouseWindowSpaceX = mouse.x - wx - hwin.dragX;
var mouseWindowSpaceY = mouse.y - wy - hwin.dragY;

return (
mouseWindowSpaceX >= 0 && mouseWindowSpaceX < hwin.lastMaxX
&& mouseWindowSpaceY >= 0 && mouseWindowSpaceY < hwin.lastMaxY
);
}

static function roundfp(f: Float, precision = 2): Float {
f *= Math.pow(10, precision);
return Math.round(f) / Math.pow(10, precision);
Expand Down
25 changes: 24 additions & 1 deletion blender/arm/logicnode/input/LN_get_mouse_started.py
@@ -1,13 +1,36 @@
from arm.logicnode.arm_nodes import *


class GetMouseStartedNode(ArmLogicTreeNode):
"""."""
bl_idname = 'LNGetMouseStartedNode'
bl_label = 'Get Mouse Started'
arm_version = 1
arm_version = 2

property0: HaxeBoolProperty(
'property0',
name='Include Debug Console',
description=(
'If disabled, this node does not react to mouse press events'
' over the debug console area. Enable this option to catch those events'
)
)

def arm_init(self, context):
self.add_input('ArmNodeSocketAction', 'In')

self.add_output('ArmNodeSocketAction', 'Out')
self.add_output('ArmStringSocket', 'Button')

def draw_buttons(self, context, layout):
layout.prop(self, 'property0')

def get_replacement_node(self, node_tree: bpy.types.NodeTree):
if self.arm_version not in (0, 1):
raise LookupError()

return NodeReplacement(
'LNGetMouseStartedNode', self.arm_version, 'LNGetMouseStartedNode', 2,
in_socket_mapping={0: 0}, out_socket_mapping={0: 0, 1: 1},
property_defaults={'property0': True}
)
31 changes: 25 additions & 6 deletions blender/arm/logicnode/input/LN_mouse.py
Expand Up @@ -6,7 +6,7 @@ class MouseNode(ArmLogicTreeNode):
bl_idname = 'LNMergedMouseNode'
bl_label = 'Mouse'
arm_section = 'mouse'
arm_version = 2
arm_version = 3

property0: HaxeEnumProperty(
'property0',
Expand All @@ -23,20 +23,39 @@ class MouseNode(ArmLogicTreeNode):
('side1', 'Side 1', 'Side 1 mouse button'),
('side2', 'Side 2', 'Side 2 mouse button')],
name='', default='left')
property2: HaxeBoolProperty(
'property2',
name='Include Debug Console',
description=(
'If disabled, this node does not react to mouse press events'
' over the debug console area. Enable this option to catch those events'
)
)

def arm_init(self, context):
self.add_output('ArmNodeSocketAction', 'Out')
self.add_output('ArmBoolSocket', 'State')

def draw_buttons(self, context, layout):
layout.prop(self, 'property0')
layout.prop(self, 'property1')

if self.property0 != 'moved':
layout.prop(self, 'property1')
layout.prop(self, 'property2')

def draw_label(self) -> str:
return f'{self.bl_label}: {self.property1}'

def get_replacement_node(self, node_tree: bpy.types.NodeTree):
if self.arm_version not in (0, 1):
raise LookupError()

return NodeReplacement.Identity(self)
if 0 <= self.arm_version < 2:
return NodeReplacement.Identity(self)

elif self.arm_version == 2:
return NodeReplacement(
'LNMergedMouseNode', self.arm_version, 'LNMergedMouseNode', 3,
in_socket_mapping={}, out_socket_mapping={0: 0, 1: 1},
property_mapping={'property0': 'property0', 'property1': 'property1'},
property_defaults={'property2': True}
)

raise LookupError()
@@ -1,12 +1,36 @@
from arm.logicnode.arm_nodes import *


class GetDebugConsoleSettings(ArmLogicTreeNode):
"""Returns the debug console settings."""
"""Return properties of the debug console.
@output Enabled: Whether the debug console is enabled.
@output Visible: Whether the debug console is visible,
or `false` if the debug console is disabled.
@output Hovered: Whether the debug console is hovered by the mouse cursor,
or `false` if the debug console is disabled.
@output UI Scale: The scaling factor of the debug console user interface,
or `1.0` if the debug console is disabled.
@output Position: The initial position of the debug console.
Possible values if the debug console is enabled: `"Left"`, `"Center"`, `"Right"`.
If the debug console is disabled, the returned value is an empty string `""`.
"""
bl_idname = 'LNGetDebugConsoleSettings'
bl_label = 'Get Debug Console Settings'
arm_version = 1
arm_version = 2

def arm_init(self, context):
self.add_output('ArmBoolSocket', 'Enabled')
self.add_output('ArmBoolSocket', 'Visible')
self.add_output('ArmFloatSocket', 'Scale')
self.add_output('ArmBoolSocket', 'Hovered')
self.add_output('ArmFloatSocket', 'UI Scale')
self.add_output('ArmStringSocket', 'Position')

def get_replacement_node(self, node_tree: bpy.types.NodeTree):
if self.arm_version not in (0, 1):
raise LookupError()

return NodeReplacement(
'LNGetDebugConsoleSettings', self.arm_version, 'LNGetDebugConsoleSettings', 2,
in_socket_mapping={}, out_socket_mapping={0: 1, 1: 3, 2: 4}
)

0 comments on commit cd2750c

Please sign in to comment.