Skip to content

Commit

Permalink
Scroll popup code views when the cursor moves
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrw committed Sep 14, 2023
1 parent 04eca77 commit f1bb8e5
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 38 deletions.
37 changes: 36 additions & 1 deletion app/components/SignNode.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
[gd_scene load_steps=7 format=2]
[gd_scene load_steps=11 format=2]

[ext_resource path="res://themes/mono.otf" type="DynamicFontData" id=1]
[ext_resource path="res://components/MarkdownLabel.tscn" type="PackedScene" id=5]
[ext_resource path="res://components/SignNode.gdns" type="Script" id=7]

[sub_resource type="DynamicFont" id=19]
size = 110
font_data = ExtResource( 1 )

[sub_resource type="StyleBoxFlat" id=20]
bg_color = Color( 0, 0, 0, 0 )
border_color = Color( 1, 0.45098, 0.992157, 1 )

[sub_resource type="StyleBoxFlat" id=21]
bg_color = Color( 0, 0, 0, 0.839216 )
border_color = Color( 1, 0.45098, 0.992157, 1 )

[sub_resource type="QuadMesh" id=18]
resource_local_to_scene = true

Expand Down Expand Up @@ -46,6 +59,28 @@ size_flags_vertical = 1
scroll_horizontal_enabled = false
scroll_vertical_enabled = false

[node name="TextEdit" type="TextEdit" parent="Viewport"]
anchor_right = 1.0
anchor_bottom = 1.0
margin_bottom = 2.0
custom_colors/selection_color = Color( 0.207843, 0, 0.321569, 1 )
custom_colors/executing_line_color = Color( 0.0392157, 0, 0.168627, 1 )
custom_colors/font_color_readonly = Color( 0.878431, 0.878431, 0.878431, 1 )
custom_fonts/font = SubResource( 19 )
custom_styles/read_only = SubResource( 20 )
custom_styles/normal = SubResource( 21 )
always_draw_caret = true
indent_size = 2
readonly = true
syntax_highlighting = true
show_line_numbers = true
smooth_scrolling = true
caret_blink = true
caret_moving_by_right_click = false
__meta__ = {
"_edit_use_anchors_": false
}

[node name="MeshInstance" type="MeshInstance" parent="."]
transform = Transform( -1, 0, -1.50996e-07, 0, 1, 0, 1.50996e-07, 0, -1, 0, 0, 0 )
mesh = SubResource( 18 )
Expand Down
2 changes: 1 addition & 1 deletion enu.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ else:

requires "nim >= 1.6.10",
"https://github.com/dsrw/godot-nim#892c482",
"https://github.com/dsrw/model_citizen 0.17.4",
"https://github.com/dsrw/model_citizen 0.17.5",
"https://github.com/dsrw/nanoid.nim 0.2.1",
"cligen 1.6.0",
"https://github.com/treeform/pretty",
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/script_controllers/worker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,12 @@ proc worker_thread(params: (ZenContext, GameState)) {.gcsafe.} =
worker.load_script_and_dependents(player)

var sign = Sign.init("", "", width = 3, height = 2.05, owner = state.player,
size = 244, zoomable = true, billboard = true, transform = Transform.init(origin = vec3(0, 3, 0)))
size = 244, zoomable = true, billboard = true, text_only = true,
transform = Transform.init(origin = vec3(0, 3, 0)))

state.player.units += sign
sign.global_flags -= Visible
sign.local_flags += Hide

var running = true

Expand Down
5 changes: 4 additions & 1 deletion src/core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ when not defined(no_godot):

default_chronicles_stream.output.writer =
proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe.} =
godot.print msg
when defined(release):
godot.print msg
else:
echo msg

# misc

Expand Down
6 changes: 3 additions & 3 deletions src/models/players.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ proc init*(_: type Player): Player =
id: \"player-{Zen.thread_ctx.id}",
rotation_value: ~0.0,
start_transform: Transform.init(origin = vec3(0, 1, 0)),
input_direction_value: ~Vector3
input_direction_value: ~Vector3,
cursor_position_value: ~((0, 0))
)
result.init_unit
result.global_flags += Global
Expand Down Expand Up @@ -36,7 +37,6 @@ proc `open_code=`*(self: Player, code: string) =
if code == "":
unit.global_flags -= Visible
else:
let markdown = "```nim\n" & code & "\n```"
unit.markdown = markdown
unit.markdown = code
unit.global_flags += Visible
return
5 changes: 3 additions & 2 deletions src/models/signs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import core, states, bots, builds, models / [colors, units]
proc init*(_: type Sign,
markdown: string, title = "", owner: Unit, transform = Transform.init,
width = 1.0, height = 1.0, size = 32, billboard = false,
zoomable = true): Sign =
zoomable = true, text_only = false): Sign =

let title = if title == "": markdown else: title
var self = Sign(
Expand All @@ -20,7 +20,8 @@ proc init*(_: type Sign,
frame_created: state.frame_count,
color_value: ~action_colors[black],
start_transform: transform,
owner_value: ~owner
owner_value: ~owner,
text_only: text_only
)
self.init_unit
result = self
Expand Down
10 changes: 8 additions & 2 deletions src/nodes/bot_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import pkg/godot except print
import pkg / [chroma]
import godotapi / [scene_tree, kinematic_body, material, mesh_instance, spatial,
input_event, animation_player, resource_loader, packed_scene,
spatial_material]
import globals, core, models / [colors]
spatial_material, text_edit]
import globals, core, models / [colors], ui / markdown_label
import ./ queries

gdobj BotNode of KinematicBody:
Expand Down Expand Up @@ -130,6 +130,12 @@ gdobj BotNode of KinematicBody:
self.set_walk_animation(change.item.length,
player.input_direction.z > 0.0)

player.cursor_position_value.watch:
if added:
let editor = self.get_node("SignNode/Viewport/TextEdit") as TextEdit
editor.cursor_set_line(change.item.line, true)
editor.cursor_set_column(change.item.col, true)

self.model.scale_value.watch:
if added:
let scale = change.item
Expand Down
39 changes: 31 additions & 8 deletions src/nodes/sign_node.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pkg / [godot]
import godotapi / [spatial, resource_loader, packed_scene, collision_shape,
mesh_instance, quad_mesh, spatial_material, viewport,
style_box_flat]
import ui / markdown_label
mesh_instance, quad_mesh, spatial_material, viewport, style_box_flat,
text_edit]
import ui / [markdown_label, editor]
import core

gdobj SignNode of Spatial:
Expand All @@ -11,7 +11,9 @@ gdobj SignNode of Spatial:
var material: SpatialMaterial

proc set_visibility =
if Visible in self.model.global_flags:
if Hide in self.model.local_flags:
self.visible = false
elif Visible in self.model.global_flags:
self.visible = true
self.material.params_blend_mode = spatial_material.BLEND_MODE_MIX
elif Visible notin self.model.global_flags and God in state.local_flags:
Expand All @@ -23,12 +25,21 @@ gdobj SignNode of Spatial:
proc setup* =
debug "sign setup", sign = self.model.id
var
label = self.find_node("MarkdownLabel") as MarkdownLabel
label = self.get_node("Viewport/MarkdownLabel") as MarkdownLabel
text_edit = self.get_node("Viewport/TextEdit") as TextEdit
shape = self.find_node("CollisionShape") as CollisionShape
mesh = self.find_node("MeshInstance") as MeshInstance
viewport = self.find_node("Viewport") as Viewport
quad = mesh.mesh as QuadMesh

self.material = mesh.get_active_material(0) as SpatialMaterial
text_edit.configure_highlighting

for child in text_edit.get_children:
let child = child.as_object(Node)
if child of ScrollBar or child of HScrollBar:
# hide scrollbars
ScrollBar(child).rect_scale = vec2()

proc resize =
debug "sign resize", sign = self.model.id
Expand All @@ -50,15 +61,24 @@ gdobj SignNode of Spatial:
stylebox.content_margin_left = 80 / self.model.width
label.size = int(float(self.model.size) / self.model.width)

text_edit.visible = self.model.text_only
label.visible = not self.model.text_only

resize()
self.material.params_billboard_mode =
if self.model.billboard:
BILLBOARD_ENABLED
else:
BILLBOARD_DISABLED

label.markdown = self.model.title
label.update
if self.model.text_only:
text_edit.text = self.model.markdown
else:
if self.model.title == "":
label.markdown = self.model.markdown
else:
label.markdown = self.model.title
label.update

self.model.title_value.watch:
if added or touched:
Expand All @@ -72,7 +92,10 @@ gdobj SignNode of Spatial:
self.model.markdown_value.watch:
if added or touched:
if self.model.title == "":
label.markdown = change.item
if self.model.text_only:
text_edit.text = change.item
else:
label.markdown = change.item
resize()
label.update

Expand Down
4 changes: 3 additions & 1 deletion src/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type
Global, Visible, Lock, Ready, ScriptInitializing, Dirty

LocalModelFlags* = enum
Hover, TargetMoved, Highlight
Hover, TargetMoved, Highlight, Hide

ConsoleModel* = ref object
log*: ZenSeq[string]
Expand Down Expand Up @@ -117,6 +117,7 @@ type
colliders*: HashSet[Model]
rotation_value*: ZenValue[float]
input_direction_value*: ZenValue[Vector3]
cursor_position_value*: ZenValue[tuple[line: int, col: int]]

Bot* = ref object of Unit
animation_value*: ZenValue[string]
Expand All @@ -127,6 +128,7 @@ type
size_value*: ZenValue[int]
billboard_value*, zoomable_value*: ZenValue[bool]
owner_value*: ZenValue[Unit]
text_only*: bool

VoxelKind* = enum
Hole, Manual, Computed
Expand Down
24 changes: 14 additions & 10 deletions src/ui/editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import godotapi / [text_edit, scene_tree, node, input_event, global_constants,
import core, globals
import models except Color

proc configure_highlighting*(self: TextEdit) =
# strings
self.add_color_region("\"", "\"", ir_black[text], false)
self.add_color_region("\"\"\"", "\"\"\"", ir_black[text], false)
# block comments
self.add_color_region("#[", "]#", ir_black[comment], false)
# line comments
self.add_color_region("#", "\n", ir_black[comment], true)

gdobj Editor of TextEdit:
var
comment_color* {.gdExport.} = init_color(0.5, 0.5, 0.5)
Expand Down Expand Up @@ -50,15 +59,6 @@ gdobj Editor of TextEdit:
state.open_unit = nil
self.get_tree().set_input_as_handled()

proc configure_highlighting =
# strings
self.add_color_region("\"", "\"", ir_black[text], false)
self.add_color_region("\"\"\"", "\"\"\"", ir_black[text], false)
# block comments
self.add_color_region("#[", "]#", self.comment_color, false)
# line comments
self.add_color_region("#", "\n", self.comment_color, true)

proc clear_errors =
for i in 0..<self.get_line_count():
self.set_line_as_marked(i, false)
Expand All @@ -78,8 +78,12 @@ gdobj Editor of TextEdit:
method on_text_changed*() =
state.player.open_code = self.text

method on_cursor_changed*() =
state.player.cursor_position =
(int self.cursor_get_line, int self.cursor_get_column)

method ready* =
self.bind_signals(self, "text_changed")
self.bind_signals(self, "text_changed", "cursor_changed")
var stylebox = self.get_stylebox("normal").as(StyleBoxFlat)
self.og_bg_color = stylebox.bg_color

Expand Down
7 changes: 2 additions & 5 deletions src/ui/markdown_label.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import std / [lists, algorithm, tables]
import pkg / [godot, markdown]
import godotapi / [rich_text_label, scroll_container, text_edit, theme,
dynamic_font, dynamic_font_data, style_box_flat, main_loop]
import core, globals
import core, globals, ui / editor
import models / colors except Color

export scroll_container
Expand Down Expand Up @@ -87,10 +87,7 @@ gdobj MarkdownLabel of ScrollContainer:

proc add_text_edit(): TextEdit =
result = self.og_text_edit.duplicate as TextEdit
result.add_color_region("\"\"\"", "\"\"\"", ir_black[normal], false)
result.add_color_region("\"", "\"", ir_black[text], false)
result.add_color_region("#", "\n", comment_color, true)
result.add_color_region("#[", "]#", comment_color, false)
result.configure_highlighting
if not ?self.current_label:
# Don't add borders if the only thing in our doc is code
var stylebox = result.get_stylebox("normal").duplicate.as(StyleBoxFlat)
Expand Down
10 changes: 8 additions & 2 deletions src/ui/right_panel.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ proc set_filter(self: Control, filter: int64) =
if child of Control:
set_filter(Control(child), filter)

proc md(self: Sign, md: string): string =
if self.text_only:
"```nim\n" & md & "\n```"
else:
md

gdobj RightPanel of MarginContainer:
var label: MarkdownLabel
var zid: ZID
Expand All @@ -21,11 +27,11 @@ gdobj RightPanel of MarginContainer:
if added and change.item != nil:
state.push_flags DocsVisible, DocsFocused
var sign = change.item
self.label.markdown = sign.markdown
self.label.markdown = md(sign, sign.markdown)
self.label.update
self.zid = sign.markdown_value.changes:
if added:
self.label.markdown = change.item
self.label.markdown = md(sign, change.item)
self.label.update
if removed and change.item != nil:
change.item.markdown_value.untrack(self.zid)
Expand Down
2 changes: 1 addition & 1 deletion vendor/godot

0 comments on commit f1bb8e5

Please sign in to comment.