Skip to content

Commit

Permalink
improvement(lesson15): rethink color coding and box alignment
Browse files Browse the repository at this point in the history
closes #430
  • Loading branch information
razcore-rad committed Mar 8, 2022
1 parent 7a046a7 commit 766b821
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 179 deletions.
58 changes: 29 additions & 29 deletions course/course-learn-gdscript.tres
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
[gd_resource type="Resource" load_steps=30 format=2]

[ext_resource path="res://resources/Course.gd" type="Script" id=1]
[ext_resource path="res://course/lesson-26-looping-over-dictionaries/lesson.tres" type="Resource" id=2]
[ext_resource path="res://course/lesson-14-multiplying/lesson.tres" type="Resource" id=3]
[ext_resource path="res://course/lesson-21-strings/lesson.tres" type="Resource" id=4]
[ext_resource path="res://course/lesson-2-your-first-error/lesson.tres" type="Resource" id=5]
[ext_resource path="res://course/lesson-11-time-delta/lesson.tres" type="Resource" id=6]
[ext_resource path="res://course/lesson-17-while-loops/lesson.tres" type="Resource" id=7]
[ext_resource path="res://course/lesson-6-multiple-function-parameters/lesson.tres" type="Resource" id=8]
[ext_resource path="res://course/lesson-15-modulo/lesson.tres" type="Resource" id=9]
[ext_resource path="res://course/lesson-13-conditions/lesson.tres" type="Resource" id=10]
[ext_resource path="res://course/lesson-27-value-types/lesson.tres" type="Resource" id=11]
[ext_resource path="res://course/lesson-22-functions-return-values/lesson.tres" type="Resource" id=12]
[ext_resource path="res://course/lesson-18-for-loops/lesson.tres" type="Resource" id=13]
[ext_resource path="res://course/lesson-3-standing-on-shoulders-of-giants/lesson.tres" type="Resource" id=14]
[ext_resource path="res://course/lesson-28-specifying-types/lesson.tres" type="Resource" id=15]
[ext_resource path="res://course/lesson-10-the-game-loop/lesson.tres" type="Resource" id=16]
[ext_resource path="res://course/lesson-20-looping-over-arrays/lesson.tres" type="Resource" id=17]
[ext_resource path="res://course/lesson-23-append-to-arrays/lesson.tres" type="Resource" id=18]
[ext_resource path="res://course/lesson-4-drawing-a-rectangle/lesson.tres" type="Resource" id=19]
[ext_resource path="res://course/lesson-24-access-array-indices/lesson.tres" type="Resource" id=20]
[ext_resource path="res://course/lesson-19-creating-arrays/lesson.tres" type="Resource" id=21]
[ext_resource path="res://course/lesson-9-adding-and-subtracting/lesson.tres" type="Resource" id=22]
[ext_resource path="res://course/lesson-5-your-first-function/lesson.tres" type="Resource" id=23]
[ext_resource path="res://course/lesson-16-2d-vectors/lesson.tres" type="Resource" id=24]
[ext_resource path="res://course/lesson-8-defining-variables/lesson.tres" type="Resource" id=25]
[ext_resource path="res://course/lesson-12-using-variables/lesson.tres" type="Resource" id=26]
[ext_resource path="res://course/lesson-7-member-variables/lesson.tres" type="Resource" id=27]
[ext_resource path="res://course/lesson-25-creating-dictionaries/lesson.tres" type="Resource" id=28]
[ext_resource path="res://course/lesson-1-what-code-is-like/lesson.tres" type="Resource" id=29]
[ext_resource path="res://course/lesson-11-time-delta/lesson.tres" type="Resource" id=2]
[ext_resource path="res://course/lesson-7-member-variables/lesson.tres" type="Resource" id=3]
[ext_resource path="res://course/lesson-25-creating-dictionaries/lesson.tres" type="Resource" id=4]
[ext_resource path="res://course/lesson-1-what-code-is-like/lesson.tres" type="Resource" id=5]
[ext_resource path="res://course/lesson-23-append-to-arrays/lesson.tres" type="Resource" id=6]
[ext_resource path="res://course/lesson-13-conditions/lesson.tres" type="Resource" id=7]
[ext_resource path="res://course/lesson-27-value-types/lesson.tres" type="Resource" id=8]
[ext_resource path="res://course/lesson-2-your-first-error/lesson.tres" type="Resource" id=9]
[ext_resource path="res://course/lesson-20-looping-over-arrays/lesson.tres" type="Resource" id=10]
[ext_resource path="res://course/lesson-28-specifying-types/lesson.tres" type="Resource" id=11]
[ext_resource path="res://course/lesson-4-drawing-a-rectangle/lesson.tres" type="Resource" id=12]
[ext_resource path="res://course/lesson-24-access-array-indices/lesson.tres" type="Resource" id=13]
[ext_resource path="res://course/lesson-21-strings/lesson.tres" type="Resource" id=14]
[ext_resource path="res://course/lesson-19-creating-arrays/lesson.tres" type="Resource" id=15]
[ext_resource path="res://course/lesson-9-adding-and-subtracting/lesson.tres" type="Resource" id=16]
[ext_resource path="res://course/lesson-5-your-first-function/lesson.tres" type="Resource" id=17]
[ext_resource path="res://course/lesson-16-2d-vectors/lesson.tres" type="Resource" id=18]
[ext_resource path="res://course/lesson-8-defining-variables/lesson.tres" type="Resource" id=19]
[ext_resource path="res://course/lesson-12-using-variables/lesson.tres" type="Resource" id=20]
[ext_resource path="res://course/lesson-3-standing-on-shoulders-of-giants/lesson.tres" type="Resource" id=21]
[ext_resource path="res://course/lesson-26-looping-over-dictionaries/lesson.tres" type="Resource" id=22]
[ext_resource path="res://course/lesson-22-functions-return-values/lesson.tres" type="Resource" id=23]
[ext_resource path="res://course/lesson-18-for-loops/lesson.tres" type="Resource" id=24]
[ext_resource path="res://course/lesson-6-multiple-function-parameters/lesson.tres" type="Resource" id=25]
[ext_resource path="res://course/lesson-10-the-game-loop/lesson.tres" type="Resource" id=26]
[ext_resource path="res://course/lesson-17-while-loops/lesson.tres" type="Resource" id=27]
[ext_resource path="res://course/lesson-14-multiplying/lesson.tres" type="Resource" id=28]
[ext_resource path="res://course/lesson-15-modulo/lesson.tres" type="Resource" id=29]

[resource]
script = ExtResource( 1 )
title = "Learn GDScript From Zero"
lessons = [ ExtResource( 29 ), ExtResource( 5 ), ExtResource( 14 ), ExtResource( 19 ), ExtResource( 23 ), ExtResource( 8 ), ExtResource( 27 ), ExtResource( 25 ), ExtResource( 22 ), ExtResource( 16 ), ExtResource( 6 ), ExtResource( 26 ), ExtResource( 10 ), ExtResource( 3 ), ExtResource( 9 ), ExtResource( 24 ), ExtResource( 7 ), ExtResource( 13 ), ExtResource( 21 ), ExtResource( 17 ), ExtResource( 4 ), ExtResource( 12 ), ExtResource( 18 ), ExtResource( 20 ), ExtResource( 28 ), ExtResource( 2 ), ExtResource( 11 ), ExtResource( 15 ) ]
lessons = [ ExtResource( 5 ), ExtResource( 9 ), ExtResource( 21 ), ExtResource( 12 ), ExtResource( 17 ), ExtResource( 25 ), ExtResource( 3 ), ExtResource( 19 ), ExtResource( 16 ), ExtResource( 26 ), ExtResource( 2 ), ExtResource( 20 ), ExtResource( 7 ), ExtResource( 28 ), ExtResource( 29 ), ExtResource( 18 ), ExtResource( 27 ), ExtResource( 24 ), ExtResource( 15 ), ExtResource( 10 ), ExtResource( 14 ), ExtResource( 23 ), ExtResource( 6 ), ExtResource( 13 ), ExtResource( 4 ), ExtResource( 22 ), ExtResource( 8 ), ExtResource( 11 ) ]
2 changes: 1 addition & 1 deletion course/documentation.csv
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ str,,,"Returns the argument converted into a [code]String[/code]. Works with the
int,,,"Returns the argument converted into an [code]int[/code] (whole number) [i]if possible[/i]. Supports converting decimal numbers, strings, and booleans. Useful to convert player text input into numbers.",method,
place_unit,,"cell: Vector2, unit_type: String","Creates a new unit matching the type parameter and places it at the desired cell position on the game grid.",method,
display_item,,"item: String, amount: int","Creates a new item and displays it in the inventory.",method,
add_item,,"item: String, amount: int","Increases the item count by amount.",method,
add_item,,"item: String, amount: int","Increases the item count by amount.",method,
10 changes: 5 additions & 5 deletions course/lesson-15-modulo/visuals/ExampleModulo.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ extends RunnableCodeExample

func _ready():
yield(self, \"ready\")
var number_slider = create_slider_for(\"number\", 1, 7, 1, _scene_instance.COLOR_NUMBER)
var modulo_slider = create_slider_for(\"modulo\", 1, 7, 1, _scene_instance.COLOR_MODULO)
var number_slider = create_slider_for(\"number\", 1, 7, 1, _scene_instance.number_color)
var modulo_slider = create_slider_for(\"modulo\", 1, 7, 1, _scene_instance.modulo_color)


func _set_instance_value(value: float, property_name: String, value_label: Label) -> void:
Expand All @@ -26,15 +26,15 @@ margin_bottom = 14.0
[node name="RunnableCodeExample" parent="." instance=ExtResource( 1 )]
margin_left = 7.0
margin_top = 7.0
margin_right = 307.0
margin_right = 387.0
margin_bottom = 387.0
script = SubResource( 1 )
scene = ExtResource( 2 )
[node name="Frame" parent="RunnableCodeExample" index="0"]
margin_right = 300.0
margin_right = 380.0
margin_bottom = 380.0
rect_min_size = Vector2( 300, 380 )
rect_min_size = Vector2( 380, 380 )
[node name="ResetButton" parent="RunnableCodeExample/Frame/HBoxContainer" index="0"]
visible = false
Expand Down
92 changes: 34 additions & 58 deletions course/lesson-15-modulo/visuals/Modulo.gd
Original file line number Diff line number Diff line change
@@ -1,84 +1,60 @@
tool
extends Node2D

const COLOR_DEFAULT := Color("928fb8")
const COLOR_NUMBER := Color("f5fafa")
const COLOR_MODULO := Color("fff540")
const COLOR_REMAINDER := Color("26c6f7")

export var _offset := Vector2.ZERO
export (int, 1, 7) var number := 5 setget _set_number, get_number
export (int, 1, 7) var modulo := 3 setget _set_modulo, get_modulo
const NORMAL_STYLEBOX := preload("res://course/lesson-15-modulo/visuals/Modulo/normal_stylebox.tres")
const REMAINDER_STYLEBOX := preload("res://course/lesson-15-modulo/visuals/Modulo/remainder_stylebox.tres")
const MODULO_SIZE_X := 48
const MODULO_GAP := 4

onready var _blocks := $Blocks as Node2D
export (int, 1, 7) var number := 5 setget _set_number
export (int, 1, 7) var modulo := 3 setget _set_modulo

var number_color := Color.black
var modulo_color := Color.black

onready var _result := $Result as Control
onready var _result_blocks := $Result/HBoxContainerBlocks as HBoxContainer
onready var _result_modulo := $Result/HBoxContainerModulo as HBoxContainer
onready var _string := $String as RichTextLabel
onready var _remainder := $Remainder as Label


func _ready() -> void:
_remainder.add_color_override("font_color", COLOR_REMAINDER)
for block in _blocks.get_children():
block.get_node("Shadow").self_modulate = COLOR_NUMBER
_align_blocks()
number_color = NORMAL_STYLEBOX.border_color
modulo_color = _result_modulo.get_child(0).get_stylebox("panel").border_color
_update_visual()


func _align_blocks() -> void:
for c in _blocks.get_children():
c.visible = false

var row := 0
var column := 0
for i in range(max(number, modulo)):
_blocks.get_child(i).position = Vector2(40 * column, row * 40) + _offset
row += 1
if row % modulo == 0 and modulo < number:
column += 1
row = 0


func _update_visual() -> void:
if number >= modulo:
for i in range(number):
_blocks.get_child(i).visible = false
_blocks.get_child(i).get_node("Shadow").self_modulate = COLOR_NUMBER
if i < number - (number % modulo):
_blocks.get_child(i).visible = true
_blocks.get_child(i).self_modulate = COLOR_DEFAULT
else:
_blocks.get_child(i).visible = true
_blocks.get_child(i).self_modulate = COLOR_REMAINDER
else:
for i in range(modulo):
_blocks.get_child(i).visible = true
_blocks.get_child(i).self_modulate = COLOR_REMAINDER
_blocks.get_child(i).get_node("Shadow").self_modulate = COLOR_MODULO
if i >= number:
_blocks.get_child(i).self_modulate = COLOR_DEFAULT

_string.bbcode_text = "[center][color=#%s]%s[/color] %% [color=#%s]%s[/color][/center]" % [COLOR_NUMBER.to_html(), number, COLOR_MODULO.to_html(), modulo]
var remainder := number % modulo
var cap := max(number, remainder)
for i in range(_result_blocks.get_child_count()):
var block: Panel = _result_blocks.get_child(i)
block.visible = i < cap
block.add_stylebox_override("panel", NORMAL_STYLEBOX if i < number - remainder else REMAINDER_STYLEBOX)

var rest := int(number / modulo)
for i in range(_result_modulo.get_child_count()):
var modulo_highlight: Panel = _result_modulo.get_child(i)
modulo_highlight.visible = i < rest
modulo_highlight.rect_min_size.x = MODULO_SIZE_X * modulo + MODULO_GAP * (modulo - 1)

_string.bbcode_text = (
"[center][color=#%s]%s[/color] %% [color=#%s]%s[/color][/center]" %
[number_color.to_html(), number, modulo_color.to_html(), modulo]
)
_remainder.text = "Remainder: %s" % [number % modulo]


func _set_number(value: int) -> void:
number = value
if not _blocks:
if not _result:
return
_align_blocks()
_update_visual()


func _set_modulo(value: int) -> void:
modulo = value
if not _blocks:
if not _result:
return
_align_blocks()
_update_visual()


func get_number() -> int:
return number


func get_modulo() -> int:
return modulo
Loading

0 comments on commit 766b821

Please sign in to comment.