added chage of zValue to Token
[gotb.git] / Track.rb
blobda2e15598ecba04a76128401e0c40555727fb4e6
1 module GOTB
3 module House
4         EMPTY           = -1
5         BARATHEON = 0
6         LANISTER        = 1
7         STARK           = 2
8         GREYJOY         = 3
9         TYRELL          = 4
10         MARTELL         = 5
11 end
13 HOUSE_DATA = [
14         {:token => 'data/token_baratheon.png'},
15         {:token => 'data/token_lannister.png'},
16         {:token => 'data/token_stark.png'},
17         {:token => 'data/token_greyjoy.png'},
18         {:token => 'data/token_tyrell.png'},
19         {:token => 'data/token_martell.png'}
22 class Tracks < Qt::Widget
23         def initialize
24                 super
26                 @tracks = []
28                 ll = Qt::VBoxLayout.new
30                 1.times {|i|
31                         @tracks << Track.new(i)
33                         btn = Qt::PushButton.new("ready #{i}")
34                         btn.setSizePolicy(Qt::SizePolicy::Minimum, Qt::SizePolicy::Expanding)
35                         btn.connect(:clicked, @tracks.last, :updateOrder)
37                         l = Qt::HBoxLayout.new
38                         l.addWidget(@tracks.last)
39                         l.addWidget(btn)
41                         w = Qt::Widget.new
42                         w.layout = l
43                         
44                         ll.addWidget(w)
45                 }
47                 self.layout = ll
48         end
49 end
51 class Track < Qt::GraphicsView
52         THRONE  = 0
53         FIEFDOM = 1
54         COURT           = 2
56         attr_accessor :scene,:order,:tokens,:view,:type
58         slots :updateOrder
60         TRACK_W = 1150
61         TRACK_H = 146
63         TRACK_W_2 = 624
64         TRACK_H_2 = 133
66         TOKEN_W = 100
67         TOKEN_H = 95
69         def initialize(kind, players = 6, parent = 0)
70                 #super(parent)
71                 super()
73                 @kind = kind
74                 @players = players
76                 pix = case @kind
77                         when THRONE then Qt::Pixmap.new("data/track_throne.png")
78                         when FIEFDOM then Qt::Pixmap.new("data/track_fiefdom.png")
79                         when COURT then Qt::Pixmap.new("data/track_court.png")
80                         else puts "Track: invalid type #{type}"
81                 end
82                 #pix = Qt::Pixmap.new("data/map_no_tracks_no_wild.jpg")
83                 #pix = Qt::Pixmap.new("data/map_no_tracks.jpg")
84                 #bg = Qt::GraphicsPixmapItem.new(pix)
85                 @bg = Over.new(pix)
86                 #@bg.graphicsEffect = Qt::GraphicsBlurEffect.new
87                 @bg.graphicsEffect = Qt::GraphicsOpacityEffect.new
88                 @bg.graphicsEffect.opacity = 0.4
90                 @tokens = []
91                 @players.times {|i|
92 #                       @order[i] = House::BARATHEON + i;
93                         @tokens << Token.new(House::BARATHEON + i, @bg)
94                         #@tokens.last.setFlags(Qt::GraphicsItem::ItemIsMovable | Qt::GraphicsItem::ItemSendsGeometryChanges)
95                         #@tokens.last.setPos(40+152*i,15)
96                         @tokens.last.setPos(240+150*i, 22)
97                 }
99                 @scene = Qt::GraphicsScene.new
100                 @scene.setSceneRect(0,0,TRACK_W,TRACK_H)
101                 #@scene.setSceneRect(0,0,pix.width,pix.height)
102                 @scene.addItem(@bg)
104                 self.setScene(@scene)
106                 self.verticalScrollBarPolicy = Qt::ScrollBarAlwaysOff
107                 self.horizontalScrollBarPolicy = Qt::ScrollBarAlwaysOff
109                 @high = true
110                 self.setInteractive(@high)
111         end
113         def updateOrder()
114                 puts "updateOrder"
115                 @players.times {|i|
116                         cx = 240 + 150 * i
117                         cy = 22
118                         ni = Hash[@tokens.each_with_index.map{|t,j| [j, Math.hypot(cx - t.x, cy - t.y)]}].min_by{|k,v| v}[0]
119                         @tokens[i],@tokens[ni] = @tokens[ni],@tokens[i]
120                         @tokens[i].x = cx
121                         @tokens[i].y = cy
122                 }
124                 @high = !@high
125                 setInteractive(@high)
127                 puts "new order: #{@tokens.map {|t| t.house}.join(',')}"
128         end
130         def setInteractive(enabled)
131                 @bg.graphicsEffect.enabled = !enabled
132                 @bg.enabled = enabled
133         end
135         def resizeEvent(event)
136                 self.fitInView(@bg)
137         end
140 class Token < Qt::GraphicsPixmapItem
141         attr_reader :house
143         def initialize(house, p = 0)
144                 @house = house
145                 pix = Qt::Pixmap.new(HOUSE_DATA[@house][:token])
147                 super(pix, p)
149                 @finished = false
151                 self.setFlags(Qt::GraphicsItem::ItemIsMovable | Qt::GraphicsItem::ItemSendsGeometryChanges)
152         end
154         def mousePressEvent(event)
155                 self.zValue = 1
157                 super(event)
158         end
160         def mouseReleaseEvent(event)
161                 @finished = false if (@finished)
162                 self.zValue = 0
164                 super(event)
165         end
167         def itemChange(change, value)
168                 @finished = true if (change == Qt::GraphicsItem::ItemPositionHasChanged)
169                 
170                 return super(change, value)
171         end
174 class Over < Qt::GraphicsPixmapItem
177 class TrackStars < Track
178         STARS_A = [3,2,1,0,0,0]
179         STARS_B = [3,3,2,1,0,0]
181         def initialize
182                 super
184                 @stars = STARS_A
185         end
187         def setStars(ppl)
188                 if (ppl >= 5)
189                         @stars = STARS_B
190                 else 
191                         @stars = STARS_A
192                 end
193         end