差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

次のリビジョン
前のリビジョン
fltk:example-app-countdown_timer [2025/05/27 07:48] – 作成 freemikanfltk:example-app-countdown_timer [2025/06/12 11:45] (現在) – [スクリーンショット] Remove imagebox freemikan
行 1: 行 1:
-====== プルアプリケーショ: BMI計算機 ====== +====== 例: カウトダウタイマー ====== 
-{{:fltk:fltk_shadow.png?200|FLTK}}+[[fltk:|{{:fltk:fltk_shadow.png?200|}}]]
  
  
 ===== スクリーンショット ===== ===== スクリーンショット =====
- +{{ :fltk:countdown_timer_app.gif |カウントダウンタイマー}}
 ===== プロジェクトレイアウト ===== ===== プロジェクトレイアウト =====
  
行 41: 行 40:
 #include <FL/Fl.H> #include <FL/Fl.H>
 #include <FL/Fl_Box.H> #include <FL/Fl_Box.H>
-#include <FL/Fl_Window.H> 
 #include <FL/Fl_Button.H> #include <FL/Fl_Button.H>
 +#include <FL/Fl_Window.H>
  
-#include <array+#include <format
-#include <cstdio>+#include <string>
  
 struct AppContext { struct AppContext {
   int start = 10;   int start = 10;
   int count = 0;   int count = 0;
-  Fl_Box *countdown_digits;+  Fl_Box *digits;
   Fl_Button *start_button;   Fl_Button *start_button;
   Fl_Button *reset_button;   Fl_Button *reset_button;
-  std::array<char, 100> cbuf;+  std::string sbuf;
 }; };
  
-AppContext *as_app_context(void *ptr) { +AppContext *as_app_context(void *ptr) { return static_cast<AppContext *>(ptr); }
-  return static_cast<AppContext *>(ptr); +
-}+
  
-void update_countdown_digits_label(AppContext *ctx) { +void update_digits_label(AppContext *ctx) { 
-  snprintf(ctx->cbuf.data(), sizeof(ctx->cbuf), "%d", ctx->count); +  ctx->sbuf.clear()
-  ctx->countdown_digits->label(ctx->cbuf.data());+  std::format_to(std::back_inserter(ctx->sbuf), "{}", ctx->count); 
 +  ctx->digits->label(ctx->sbuf.c_str());
 } }
  
 void timeout_cb(void *data) { void timeout_cb(void *data) {
   auto ctx = as_app_context(data);   auto ctx = as_app_context(data);
-  +
   if (ctx->count <= 0) {   if (ctx->count <= 0) {
-    ctx->start_button->activate();+    // Finished 
 +    ctx->start_button->label("スタート");
     return;     return;
   }   }
行 75: 行 74:
   --ctx->count;   --ctx->count;
  
-  update_countdown_digits_label(ctx); +  update_digits_label(ctx); 
-  +
   Fl::repeat_timeout(1.0, timeout_cb, ctx);   Fl::repeat_timeout(1.0, timeout_cb, ctx);
 } }
  
 void start_button_cb(Fl_Widget *w, void *data) { void start_button_cb(Fl_Widget *w, void *data) {
-  Fl::add_timeout(1.0, timeout_cb, data); +  if (!Fl::has_timeout(timeout_cb, data)) { 
-  w->deactivate();+    // Start button pressed 
 +    Fl::add_timeout(1.0, timeout_cb, data); 
 +    w->label("一時停止"); 
 +  } else { 
 +    // Pause button pressed 
 +    Fl::remove_timeout(timeout_cb, data); 
 +    w->label("スタート"); 
 +  }
 } }
  
行 88: 行 94:
   auto ctx = as_app_context(data);   auto ctx = as_app_context(data);
   ctx->count = ctx->start;   ctx->count = ctx->start;
-  update_countdown_digits_label(ctx);+  update_digits_label(ctx);
 } }
  
 int main() { int main() {
-  auto window = new Fl_Window(600600, "Timer foo"); +  auto window = new Fl_Window(300300, "Countdown Timer"); 
-   + 
-  auto countdown_digits = new Fl_Box(200200200200); +  auto digits = new Fl_Box(1010280240); 
-  countdown_digits->box(FL_DOWN_BOX); +  digits->box(FL_DOWN_BOX); 
-  countdown_digits->labelfont(FL_BOLD); +  digits->labelfont(FL_BOLD); 
-  countdown_digits->labelsize(80); +  digits->labelsize(80); 
-  countdown_digits->labeltype(FL_SHADOW_LABEL);+  digits->labeltype(FL_SHADOW_LABEL); 
 + 
 +  auto start_button = new Fl_Button(10, 260, 140, 30, "スタート"); 
 +  auto reset_button = new Fl_Button(150, 260, 140, 30, "リセット");
  
-  auto start_button = new Fl_Button(210, 420, 80, 30, "スタート"); 
-  auto reset_button = new Fl_Button(310, 420, 80, 30, "リセット"); 
-   
   int const countdown_start = 10;   int const countdown_start = 10;
-  +
   AppContext ctx{   AppContext ctx{
-    .start = countdown_start, +      .start = countdown_start, 
-    .count = countdown_start, +      .count = countdown_start, 
-    .countdown_digits countdown_digits+      .digits digits
-    .start_button = start_button, +      .start_button = start_button, 
-    .reset_button = reset_button,+      .reset_button = reset_button,
   };   };
-   + 
-  update_countdown_digits_label(&ctx);+  update_digits_label(&ctx);
   start_button->callback(start_button_cb, &ctx);   start_button->callback(start_button_cb, &ctx);
   reset_button->callback(reset_button_cb, &ctx);   reset_button->callback(reset_button_cb, &ctx);
-  +
   window->end();   window->end();
   window->show();   window->show();
文書の先頭へ