Godotの簡単なサンプルを置いています。(Godot4.X)
波形作成
ランダムな値を使って線を描きます。スライダーによる調整も入れています。
extends Node2D
# 波形更新速度(秒)
var wave_update_speed: float = 0.1
# 波形のY座標値を保持
var Yval: Array = []
# 表示するデータの最大行数
var max_display_lines: int = 100
# 累積表示用データ
var displayed_lines: Array = []
# 波形の描画基準位置
var base_draw_position: int = 400
# 波形の点と点の間隔
var wave_spacing: int = 100
func _ready():
# スライダーの初期値を設定
$SpeedSlider.value = ($SpeedSlider.min_value + $SpeedSlider.max_value) / 2
$PositionSlider.value = base_draw_position
$SpacingSlider.value = wave_spacing
# タイマーの初期設定
set_wave_update_speed($SpeedSlider.value)
# 初期ラベルの更新
update_speed_label($SpeedSlider.value)
update_position_label($PositionSlider.value)
update_spacing_label($SpacingSlider.value)
update_wave_data_label()
# 初期状態のポイントを設定
for i in range(10):
Yval.append(0) # 0を10回追加
$Line2D.add_point(Vector2(i * wave_spacing, base_draw_position))
var cnt: int = 0 # データの累積カウント
# Timerのtimeoutで波形を更新
func _on_Timer_timeout():
if Yval.size() < 10:
return # 必要な要素がない場合は終了
# ランダム値を生成して配列を更新
var buf = randi_range(-10, 10)
var buf0 = buf * 10
add_to_array(buf0)
cnt += 1 # データのカウントを増やす
# Line2Dのポイントを更新
for i in range(Yval.size()):
$Line2D.points[i] = Vector2(1000 + i * -wave_spacing, Yval[i] + base_draw_position)
# 波形データラベルを更新
update_wave_data_label()
# 配列の先頭に新しい値を追加し、最後の値を削除
func add_to_array(new_value):
# 配列の先頭に新しい値を追加
Yval.insert(0, new_value)
# 配列の最大サイズを超えた場合、最後の要素を削除
if Yval.size() > 10:
Yval.pop_back()
# 波形更新速度を変更するメソッド
func set_wave_update_speed(new_speed: float):
wave_update_speed = max(new_speed, 0.01) # 最小値を0.01秒に制限
$Timer.wait_time = wave_update_speed
$Timer.start()
# スライダー値が変更されたときに呼び出される(速度)
func _on_SpeedSlider_value_changed(value):
set_wave_update_speed(value)
update_speed_label(value)
# スライダー値が変更されたときに呼び出される(描画位置)
func _on_PositionSlider_value_changed(value):
base_draw_position = value
update_position_label(value)
# スライダー値が変更されたときに呼び出される(間隔)
func _on_SpacingSlider_value_changed(value):
wave_spacing = value
update_spacing_label(value)
# 配列サイズをチェックしてから処理を実行
if Yval.size() < 10 or $Line2D.points.size() < 10:
return # 必要なデータが揃っていない場合は終了
# 点と点の間隔を即座に反映
for i in range(10):
$Line2D.points[i] = Vector2(1000 + i * -wave_spacing, Yval[i] + base_draw_position)
# ラベルを更新(速度)
func update_speed_label(value: float):
$SpeedLabel.text = "Speed: %.2f seconds" % value
# ラベルを更新(描画位置)
func update_position_label(value: int):
$PositionLabel.text = "Position: %d" % value
# ラベルを更新(間隔)
func update_spacing_label(value: int):
$SpacingLabel.text = "Spacing: %d" % value
# ラベルを更新(波形データ)
func update_wave_data_label():
if Yval.size() > 0:
# 新しいデータ行を作成(累積データ数を含む)
var new_line = "Line %d (Total: %d): %d" % [displayed_lines.size() + 1, cnt, Yval[0]]
displayed_lines.append(new_line)
# 最大行数を超えたら古い行を削除
if displayed_lines.size() > max_display_lines:
displayed_lines.pop_front()
# ラベルに累積データを設定
$ScrollContainer/WaveDataLabel.text = "\n".join(displayed_lines)
# スクロール位置を更新(常に一番下にスクロール)
var scrollbar = $ScrollContainer.get_v_scroll()
if scrollbar and scrollbar is VScrollBar:
scrollbar.value = scrollbar.max_value
GitHub - WOCae/L031
Contribute to WOCae/L031 development by creating an account on GitHub.
ファイル操作
extends Control
# ファイルダイアログ、ボタン、テキスト編集領域への参照
@onready var open_file_dialog = $OpenFileDialog
@onready var save_file_dialog = $SaveFileDialog
@onready var open_button = $OpenButton
@onready var save_button = $SaveButton
@onready var text_edit = $TextEdit
# The URL we will connect to.
@export var websocket_url = "https://backyard.enginfo.jp:8080"
@onready var socket = WebSocketPeer.new()
#@onready var websocket = WebSocketPeer.new("https://backyard.enginfo.jp:8080")
func _ready():
# ファイルダイアログの設定
open_file_dialog.mode = 0 # ファイル選択モード
open_file_dialog.access = FileDialog.ACCESS_FILESYSTEM
open_file_dialog.filters = ["*.txt", "*.*"]
save_file_dialog.mode = 2 # ファイル保存モード
save_file_dialog.access = FileDialog.ACCESS_FILESYSTEM
save_file_dialog.filters = ["*.txt", "*.*"]
# シグナルの接続
open_file_dialog.file_selected.connect(_on_file_open_selected)
save_file_dialog.file_selected.connect(_on_file_save_selected)
open_button.pressed.connect(_on_open_button_pressed)
save_button.pressed.connect(_on_save_button_pressed)
# Initiate connection to the given URL.
var err = socket.connect_to_url(websocket_url)
if err != OK:
print("Unable to connect")
set_process(false)
else:
# Wait for the socket to connect.
await get_tree().create_timer(2).timeout
# Send data.
socket.send_text("Test packet")
func _on_open_button_pressed():
# ファイルを開くダイアログを表示
open_file_dialog.popup()
func _on_save_button_pressed():
# ファイルを保存するダイアログを表示
save_file_dialog.popup()
func _on_file_open_selected(path: String):
# ファイルを開く
var file = FileAccess.open(path, FileAccess.ModeFlags.READ)
if file:
var content = file.get_as_text() # ファイル内容を取得
file.close()
text_edit.text = content # テキスト編集領域に内容を表示
print("File content loaded successfully.")
else:
text_edit.text = "Failed to open the file."
print("Error: Unable to open the file.")
func _on_file_save_selected(path: String):
# ファイルを保存する
var file = FileAccess.open(path, FileAccess.ModeFlags.WRITE)
if file:
file.store_string(text_edit.text) # 編集内容を保存
file.close()
print("File saved successfully to: ", path)
else:
print("Error: Unable to save the file.")
func _process(delta):
# Call this in _process or _physics_process. Data transfer and state updates
# will only happen when calling this function.
socket.poll()
# get_ready_state() tells you what state the socket is in.
var state = socket.get_ready_state()
# WebSocketPeer.STATE_OPEN means the socket is connected and ready
# to send and receive data.
if state == WebSocketPeer.STATE_OPEN:
while socket.get_available_packet_count():
print("Got data from server: ", socket.get_packet().get_string_from_utf8())
$Label.text = "Got data from server: "+ socket.get_packet().get_string_from_utf8()
# WebSocketPeer.STATE_CLOSING means the socket is closing.
# It is important to keep polling for a clean close.
elif state == WebSocketPeer.STATE_CLOSING:
pass
# WebSocketPeer.STATE_CLOSED means the connection has fully closed.
# It is now safe to stop polling.
elif state == WebSocketPeer.STATE_CLOSED:
# The code will be -1 if the disconnection was not properly notified by the remote peer.
var code = socket.get_close_code()
print("WebSocket closed with code: %d. Clean: %s" % [code, code != -1])
set_process(false) # Stop processing.
$Label.text = "WebSocket closed with code: %d. Clean: %s" % [code, code != -1]
FileDialog.ACCESS_FILESYSTEM でアクセス箇所が変わります。
GitHub - WOCae/l-031_1
Contribute to WOCae/l-031_1 development by creating an account on GitHub.
コメント