OSDN Git Service

fix:
[pettanr/pettanr.git] / app / assets / javascripts / addons / circle_speech_balloons / init.js.coffee
index 38777a9..1110032 100644 (file)
@@ -82,11 +82,8 @@ class Pettanr.CircleSpeechBalloon.Speech extends Backbone.Model
     @engine_name() + '/speeches'\r
 \r
 class Pettanr.CircleSpeechBalloon.SpeechBalloonModule\r
-  supply_default: () ->\r
-    super()\r
-    sbt = @speech_balloon_template()\r
-    sbt.fetch({cache: true}).done =>\r
-      @speech_balloon_template_module_name = sbt.get('module_name')\r
+  supply_default: (templates) ->\r
+    @templates = templates\r
   \r
   speech_balloon_extend: () ->\r
     @new_speech_balloon() if not @sbex\r
@@ -102,9 +99,8 @@ class Pettanr.CircleSpeechBalloon.SpeechBalloonModule
     @sbex.supply_default()\r
   \r
 class Pettanr.CircleSpeechBalloon.BalloonModule\r
-  supply_default: () ->\r
-    super()\r
-    s = @parsed_settings()['all']['balloon']\r
+  supply_default: (templates) ->\r
+    @templates = templates\r
     @set({\r
       x: 0,\r
       y: 0,\r
@@ -112,6 +108,21 @@ class Pettanr.CircleSpeechBalloon.BalloonModule
       eight: @parsed_settings()['speech_balloon']['default_height'],\r
       r: @parsed_settings()['speech_balloon']['default_r']\r
     })\r
+    @select_system_picture()\r
+  \r
+  hoge_templates: () ->\r
+    if !@templates.speech_balloon_template\r
+      empty_item = new Pettanr.SpeechBalloonTemplate({id: @get('speech_balloon_template_id')})\r
+      item = Pettanr.cache.restore(empty_item)\r
+      if item\r
+        # got cached item\r
+        @templates.speech_balloon_template = item\r
+      else\r
+        # built empty item\r
+        item = empty_item\r
+        item.fetch().done =>\r
+          Pettanr.cache.store(item)\r
+        @templates.speech_balloon_template = item\r
   \r
   balloon_extend: () ->\r
     @new_balloon() if not @bex\r
@@ -128,25 +139,32 @@ class Pettanr.CircleSpeechBalloon.BalloonModule
   \r
   parsed_settings: () ->\r
     return @parsed if @parsed\r
-    sbt = @speech_balloon_template()\r
-    sbt.fetch({cache: true}).done =>\r
-      @parsed = sbt.parsed_settings()\r
+    @parsed = @templates.speech_balloon_template.parsed_settings()\r
   \r
   select_system_picture: () ->\r
-    tr = @get('r') % 360\r
-    tr = 360 + tr if tr < 0\r
-    offset = @r_step() / 2.0\r
-    idx = Pettanr.to_i(((tr + offset) % 360) / @r_step())\r
+    idx = @r_to_index(@round_r(@get('r')))\r
     sid = @parsed_settings()[Pettanr.to_s(idx)]['balloon']['system_picture_id']\r
-    @set('system_picture_id', sid)\r
+    @set({system_picture_id: sid}, {silent: true})\r
     sid\r
   \r
   r_step: () ->\r
     @parsed_settings()['speech_balloon']['r_step']\r
   \r
+  # round 0 ~ 359\r
+  round_r: (r) ->\r
+    tr = r % 360\r
+    tr = 360 + tr if tr < 0\r
+    tr\r
+  \r
+  r_to_index: (round_r) ->\r
+    # 0 ~ 45 -> 22.5 ~ 66.5\r
+    offset = @r_step() / 2.0\r
+    # rounded index\r
+    Math.floor(((round_r + offset) % 360) / @r_step())\r
+  \r
 class Pettanr.CircleSpeechBalloon.SpeechModule\r
-  supply_default: () ->\r
-    super()\r
+  supply_default: (templates) ->\r
+    @templates = templates\r
     s = @parsed_settings()['0']['speech']\r
     @set({\r
       x: s['x_rate'],\r
@@ -173,9 +191,7 @@ class Pettanr.CircleSpeechBalloon.SpeechModule
   \r
   parsed_settings: () ->\r
     return @parsed if @parsed\r
-    sbt = @speech_balloon_template()\r
-    sbt.fetch({cache: true}).done =>\r
-      @parsed = sbt.parsed_settings()\r
+    @parsed = @templates.speech_balloon_template.parsed_settings()\r
   \r
   arrayed_quotes: () ->\r
     q = if Pettanr.is_blank(@get('quotes'))\r