ボタンをクリックすると別ウインドウが開く。
簡単なトグル状態のウインドウの作成
最初の「Open」ボタンをクリックすると、右のウインドウが開き、左のウインドウが閉じる。「CLOSE」ボタンでそのウインドウが閉じて、左のウインドウが開く。
最初のウインドウが開いた状態にはならない。交互に開いたり、閉じたり、トグル状態になる。


filename.lua
x
30
30
1
- ウインドウの基本設定
2
-- この場合、最初のウインドウサイズ10,10は大きさとは違う。下のfloat_wnd_set_geometryが大きさになる。
3
Open_Wnd = float_wnd_create(100, 100, 1, true) -- ウインドウの大きさと枠付き
4
float_wnd_set_title(Open_Wnd, "Open Window") -- ウインドウのタイトル
5
float_wnd_set_imgui_builder(Open_Wnd, "window_One") -- 最初にビルドして表示するウインドウを指定
6
float_wnd_set_geometry(Open_Wnd, 100, 800, 300, 700) -- ウインドウの大きさ、左からの位置、下からの高さ
7
float_wnd_set_onclose(Open_Wnd, "closed_demo")
8
-- パラメーター:ウインドウのハンドル、1番目黒の四角の左側からの位置と高さ、3番目以降は四角の右側の位置と高さ。
9
----------------------------------------------------------------------------------------------------------
10
-- 最初に表示されるボタン付きウインドウ
11
function window_One(Open_Wnd, x, y)
12
-- ボタンの作成
13
imgui.SetWindowFontScale(1) -- フォントの大きさ(少数点を使える)
14
if imgui.Button("Open", 100, 24) then -- ボタンの作成
15
float_wnd_set_geometry(Open_Wnd, 100, 600, 300, 500) -- クリックで2つ目のウインドウを開く
16
float_wnd_set_imgui_builder(Open_Wnd, "window_Two") -- 2番目のウインドウをビルドする。
17
end
18
end
19
-- 2番目のウインドウ、クローズボタンで最初のボタンウインドウに戻ることが出来る。
20
function window_Two(Open_Wnd2, x, y)
21
imgui.SetWindowFontScale(1) -- 下のボタンのフォントの大きさを指定
22
if imgui.Button("CLOSE", 60, 30) then -- ボタンの大きさ
23
float_wnd_set_geometry(Open_Wnd, 100, 800, 300, 700) -- 戻ったときの1番目のウインドウを表示
24
float_wnd_set_imgui_builder(Open_Wnd, "window_One") -- 1番目のウインドウをビルドする。
25
end
26
end
27
28
function closed_demo(Open_Wnd) end
29
-- Tこの関数は、ユーザーがウィンドウを閉じるときに呼び出されます。
30
-- ウィンドウが既に破棄されているため、この関数では imgui 関数の描画または呼び出しは許可されない。
最初のウインドウを開いた状態で、2番目のウインドウを開く-1
この方法ではウインドウの左上にある赤丸から閉じると2度と開かない、必ずウインドウ内の「Close」ボタンから閉じる必要がある。これなら再度サブウインドウを何度でも閉じたり開いたりすることが出来る。
その方法は、まずカスタムdatarefを作成、その値が「0」「1」にすることでウインドウを開いたり、閉じたりしている。
下は「0」になっているのでサブウインドウは閉じている。

「Open」ボタンをクリックすると、dataref値が「1」になるのでサブウインドウが表示される。

filename.lua
1
58
58
1
-- datarefを作成して書き込み可能に設定、初期は「0」に設定
2
define_shared_DataRef("FlyWithLua/Show_hide_window", "Int") --datarefを作成登録(X-Planeの再起動が必要)
3
dataref("ShowHideWindow", "FlyWithLua/Show_hide_window", "writable") --書き込み可能に設定
4
ShowHideWindow = 0; -- 初期設定はサブウインドウの非表示。
5
6
local hide = false --隠す場合はfalseで設定
7
---------------------------------------------------------------------------------------------------
8
-- iメインウインドウ、最初から表示し続け、隠さない。
9
button_wnd = float_wnd_create(350, 150, 1, true)
10
float_wnd_set_title(button_wnd, "imgui Button")
11
float_wnd_set_imgui_builder(button_wnd, "button_demo")
12
float_wnd_set_onclick(button_wnd, "button_on_click")
13
float_wnd_set_onclose(button_wnd, "closed_demo")
14
15
function button_demo(wnd, x, y)
16
if imgui.Button("OPen", 100, 50) then
17
ShowHideWindow = 1 -- dataref値「1」は表示
18
end
19
end
20
21
function button_on_click(button_wnd, x, y, state) end
22
function closed_demo(button_wnd) end
23
-- Tこの関数は、ユーザーがウィンドウを閉じるときに呼び出されます。
24
-- ウィンドウが既に破棄されているため、この関数では imgui 関数の描画または呼び出しは許可されない。
25
---------------------------------------------------------------------------------------------------
26
-- 2番目の開け閉めするウインドウ
27
-- 下の条件になったとき、ウインドウを作成して、テキストを表示する。
28
function ishwwd_on_build(ishwwd_wnd, x, y)
29
if imgui.Button("Close", 100, 50) then
30
ShowHideWindow = 0 -- ボタンを押したときこのウインドウを閉じる
31
end
32
end
33
--------------------------------------------------------------------------------------
34
ishwwd_wnd = nil -- ウインドウは空にする
35
--------------------------------------------------------------------------------------
36
-- datarefが「1」「0」になったときの条件式
37
function ishwwd_show_wnd()
38
if ShowHideWindow == 1 and hide == false then
39
ishwwd_wnd = float_wnd_create(250, 150, 1, true)
40
float_wnd_set_title(ishwwd_wnd, "Imgui Show 1") --ウインドウのタイトル
41
float_wnd_set_position(ishwwd_wnd, 100, 300)
42
float_wnd_set_imgui_builder(ishwwd_wnd, "ishwwd_on_build") -- ウインドウの作成
43
hide = true
44
end
45
end
46
-- datarefが「0」になったらウインドウを隠す
47
function ishwwd_hide_wnd()
48
if ShowHideWindow == 0 then
49
if ishwwd_wnd then
50
float_wnd_destroy(ishwwd_wnd)
51
hide = false
52
end
53
end
54
end
55
--------------------------------------------------------------------------------------
56
do_often("ishwwd_show_wnd()")
57
do_often("ishwwd_hide_wnd()")
58
-- do_oftenはLuaコードを1秒ごとに計算する。do_every_draw()よりはフレームレートに影響がすくない。
最初のウインドウを開いた状態で、2番目のウインドウを開く-2

インプットボックスの名前が重なると同じ動きになるので名前は必ず違うボタン名にする必要がある。
Button2.lua
1
122
122
1
-- 初期設定
2
local Input_A_text = ""
3
local Input_B_text = ""
4
local Input_C_text = ""
5
local Input_D_text = ""
6
-------------------------------------------------------------------------------------------------
7
8
-- imgui を表示するためのデフォルト設定スクリプト
9
button_wnd = float_wnd_create(450, 250, 1, true)
10
float_wnd_set_title(button_wnd, "imgui Button")
11
float_wnd_set_imgui_builder(button_wnd, "button_demo")
12
float_wnd_set_onclose(button_wnd, "closed_demo")
13
14
--button_pressed = 0
15
16
function button_demo(wnd, x, y)
17
-- imgui.PushItemWidth(50)
18
19
imgui.SetCursorPosY(10) --ウインドウの端から縦に移動する距離
20
imgui.SetCursorPosX(10) --ウインドウの端から横に移動する距離
21
if imgui.Button(Input_A_text, 100, 100) then
22
23
end
24
imgui.SameLine() --横に並べる
25
imgui.SetCursorPosX(((100 + 5) * 1) + 10) --ウインドウの端から横に移動する距離
26
27
if imgui.Button(Input_B_text, 100, 100) then
28
29
end
30
31
imgui.SameLine()
32
imgui.SetCursorPosX(((100 + 5) * 2) + 10)
33
34
if imgui.Button(Input_C_text, 100, 100) then
35
36
end
37
38
imgui.SameLine()
39
imgui.SetCursorPosX(((100 + 5) * 3) + 10)
40
41
if imgui.Button(Input_D_text, 100, 100) then
42
43
end
44
--2段目、縦位置と横位置の設定
45
imgui.SetCursorPosY(((100 + 5) * 1) + 10) --2番目の行を縦に移動する距離
46
imgui.SetCursorPosX(((100 + 5) * 3) + 10)
47
48
if imgui.Button("Edit", 100, 50) then
49
float_wnd_set_geometry(PoloWindow, 250, 505, 550, 345) --飛び出す、ウインドウの位置とサイズ(左下が起点)
50
float_wnd_set_imgui_builder(PoloWindow, "popup_window") --ウインドウを作成
51
end
52
end
53
54
function closed_demo(wnd)
55
-- Tこの関数は、ユーザーがウィンドウを閉じるときに呼び出されます。
56
-- ウィンドウが既に破棄されているため、この関数では imgui 関数の描画または呼び出しは許可されない。
57
end
58
59
---------------------------------------------------------------------------------------------------
60
---------------------------------------------------------------------------------------------------
61
---------------------------------------------------------------------------------------------------
62
63
FileNamePolo = SCRIPT_DIRECTORY .. "PoloInput.save"
64
65
-- ファイルを開く
66
backupfile = io.open(FileNamePolo)
67
if backupfile ~= nil then
68
bucontent = backupfile:lines()
69
Fctr = 0
70
Input_A_text = bucontent(Fctr) --1行目の読み込み
71
Input_B_text = bucontent(Fctr + 1) --2行目の読み込み
72
Input_C_text = bucontent(Fctr + 2) --2行目の読み込み
73
Fctr = Fctr + 2
74
75
backupfile:close()
76
end
77
78
-- 画面が変化するサイズをリアルタイムにに取得
79
screen_Width, screen_Height = XPLMGetScreenSize()
80
81
-- 飛び出す、ウインドウの作成 ----------------------------------------------------------------------
82
PoloWindow = float_wnd_create(10, 10, 1, true)
83
float_wnd_set_title(PoloWindow, "Polo Input Window")
84
float_wnd_set_position(PoloWindow, 10, 10)
85
float_wnd_set_onclose(PoloWindow, "closed_PoloWindow")
86
float_wnd_set_onclick(PoloWindow, "button_on_click")
87
----------------------------------------------------------------------------------------
88
89
-- 1番目のウインドウ
90
function popup_window(PoloWindow, x, y)
91
--imgui.BeginChild("First_window_Child", 400, 800) --ウインドウの大きさ
92
imgui.SetWindowFontScale(1)
93
local changed, Input_A_text_new = imgui.InputText("Input-1", Input_A_text,18)
94
if changed then
95
Input_A_text = Input_A_text_new
96
end
97
local changed, Input_B_text_new = imgui.InputText("Input-2", Input_B_text,18)
98
if changed then
99
Input_B_text = Input_B_text_new
100
end
101
local changed, Input_C_text_new = imgui.InputText("Input-3", Input_C_text,18)
102
if changed then
103
Input_C_text = Input_C_text_new
104
end
105
106
107
if imgui.Button("SAVE", 60, 30) then
108
Write_and_Save_to_Disk() --SAVEボタンでテキストを保存
109
end
110
end
111
112
function button_on_click(PoloWindow, x, y, state) end
113
function closed_PoloWindow(wnd) end
114
115
-- ディsクへの書き込みと保存
116
function Write_and_Save_to_Disk()
117
fileES = io.open(FileNamePolo, "w")
118
fileES:write(Input_A_text .. "\n")
119
fileES:write(Input_B_text .. "\n")
120
fileES:write(Input_C_text .. "\n")
121
fileES:close()
122
end