From e9d0be1b6a8d0573cfc97bd033c514848235dfbb Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Sun, 29 May 2011 15:17:28 -0400 Subject: [PATCH] Level indicator drawing. --- AppKit/CPLevelIndicator.j | 124 ++++++++++++++++++ .../level-indicator-bezel-center.png | Bin 0 -> 126 bytes .../level-indicator-bezel-left.png | Bin 0 -> 202 bytes .../level-indicator-bezel-right.png | Bin 0 -> 216 bytes ...evel-indicator-segment-critical-center.png | Bin 0 -> 187 bytes .../level-indicator-segment-critical-left.png | Bin 0 -> 259 bytes ...level-indicator-segment-critical-right.png | Bin 0 -> 275 bytes .../level-indicator-segment-empty-center.png | Bin 0 -> 170 bytes .../level-indicator-segment-empty-left.png | Bin 0 -> 229 bytes .../level-indicator-segment-empty-right.png | Bin 0 -> 240 bytes .../level-indicator-segment-normal-center.png | Bin 0 -> 187 bytes .../level-indicator-segment-normal-left.png | Bin 0 -> 246 bytes .../level-indicator-segment-normal-right.png | Bin 0 -> 268 bytes ...level-indicator-segment-warning-center.png | Bin 0 -> 180 bytes .../level-indicator-segment-warning-left.png | Bin 0 -> 252 bytes .../level-indicator-segment-warning-right.png | Bin 0 -> 259 bytes 16 files changed, 124 insertions(+) create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-bezel-center.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-bezel-left.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-bezel-right.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-critical-center.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-critical-left.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-critical-right.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-empty-center.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-empty-left.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-empty-right.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-normal-center.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-normal-left.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-normal-right.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-warning-center.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-warning-left.png create mode 100644 AppKit/Resources/CPLevelIndicator/level-indicator-segment-warning-right.png diff --git a/AppKit/CPLevelIndicator.j b/AppKit/CPLevelIndicator.j index 96dbf6abb..e22679251 100644 --- a/AppKit/CPLevelIndicator.j +++ b/AppKit/CPLevelIndicator.j @@ -32,6 +32,14 @@ CPContinuousCapacityLevelIndicatorStyle = 1; CPDiscreteCapacityLevelIndicatorStyle = 2; CPRatingLevelIndicatorStyle = 3; +var _CPLevelIndicatorBezelColor = nil, + _CPLevelIndicatorSegmentEmptyColor = nil, + _CPLevelIndicatorSegmentNormalColor = nil, + _CPLevelIndicatorSegmentWarningColor = nil, + _CPLevelIndicatorSegmentCriticalColor = nil, + + _CPLevelIndicatorSpacing = 1; + /*! @ingroup appkit @class CPLevelIndicator @@ -50,6 +58,56 @@ CPRatingLevelIndicatorStyle = 3; int _numberOfMajorTickMarks @accessors; } ++ (void)initialize +{ + var bundle = [CPBundle bundleForClass:CPLevelIndicator]; + + _CPLevelIndicatorBezelColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices: + [ + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-bezel-left.png"] size:CGSizeMake(3.0, 18.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-bezel-center.png"] size:CGSizeMake(1.0, 18.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-bezel-right.png"] size:CGSizeMake(3.0, 18.0)] + ] + isVertical:NO + ]]; + + _CPLevelIndicatorSegmentEmptyColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices: + [ + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-empty-left.png"] size:CGSizeMake(3.0, 17.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-empty-center.png"] size:CGSizeMake(1.0, 17.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-empty-right.png"] size:CGSizeMake(3.0, 17.0)] + ] + isVertical:NO + ]]; + + _CPLevelIndicatorSegmentNormalColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices: + [ + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-normal-left.png"] size:CGSizeMake(3.0, 17.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-normal-center.png"] size:CGSizeMake(1.0, 17.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-normal-right.png"] size:CGSizeMake(3.0, 17.0)] + ] + isVertical:NO + ]]; + + _CPLevelIndicatorSegmentWarningColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices: + [ + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-warning-left.png"] size:CGSizeMake(3.0, 17.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-warning-center.png"] size:CGSizeMake(1.0, 17.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-warning-right.png"] size:CGSizeMake(3.0, 17.0)] + ] + isVertical:NO + ]]; + + _CPLevelIndicatorSegmentCriticalColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices: + [ + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-critical-left.png"] size:CGSizeMake(3.0, 17.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-critical-center.png"] size:CGSizeMake(1.0, 17.0)], + [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPLevelIndicator/level-indicator-segment-critical-right.png"] size:CGSizeMake(3.0, 17.0)] + ] + isVertical:NO + ]]; +} + - (id)initWithFrame:(CGRect)aFrame { self = [super initWithFrame:aFrame]; @@ -60,11 +118,75 @@ CPRatingLevelIndicatorStyle = 3; _maxValue = 2; _warningValue = 2; _criticalValue = 2; + + [self _init]; } return self; } +- (void)_init +{ + // TODO Make themable and style dependent. + [self setBackgroundColor:_CPLevelIndicatorBezelColor]; +} + +- (void)layoutSubviews +{ + var segmentCount = _maxValue - _minValue; + + if (segmentCount <= 0) + return; + + var filledColor = _CPLevelIndicatorSegmentNormalColor, + value = [self doubleValue]; + + if (value < _criticalValue) + filledColor = _CPLevelIndicatorSegmentCriticalColor; + else if (value < _warningValue) + filledColor = _CPLevelIndicatorSegmentWarningColor; + + for (var i = 0; i < segmentCount; i++) + { + var bezelView = [self layoutEphemeralSubviewNamed:"segment-bezel-" + i + positioned:CPWindowAbove + relativeToEphemeralSubviewNamed:nil]; + + [bezelView setBackgroundColor:(_minValue + i) < value ? filledColor : _CPLevelIndicatorSegmentEmptyColor]; + } +} + +- (CPView)createEphemeralSubviewNamed:(CPString)aName +{ + return [[CPView alloc] initWithFrame:_CGRectMakeZero()]; +} + +- (CGRect)rectForEphemeralSubviewNamed:(CPString)aViewName +{ + if (aViewName.indexOf("segment-bezel") === 0) + { + var segment = parseInt(aViewName.substring("segment-bezel-".length), 10), + segmentCount = _maxValue - _minValue; + + if (segment >= segmentCount) + return _CGRectMakeZero(); + + var bounds = [self bounds], + segmentWidth = FLOOR(bounds.size.width / segmentCount), + segmentFrame = CGRectCreateCopy([self bounds]); + + segmentFrame.size.height -= 1; + segmentFrame.origin.x = segmentWidth * segment; + // Make the last segment use up the remaining space. + segmentFrame.size.width = segment < segmentCount - 1 ? segmentWidth - _CPLevelIndicatorSpacing : bounds.size.width - segmentFrame.origin.x; + + return segmentFrame; + } + + return _CGRectMakeZero(); +} + + /* - (CPLevelIndicatorStyle)style; - (void)setLevelIndicatorStyle:(CPLevelIndicatorStyle)style; @@ -122,6 +244,8 @@ var CPLevelIndicatorStyleKey = "CPLevelIndicatorStyleKey", _numberOfTickMarks = [aCoder decodeIntForKey:CPLevelIndicatorNumberOfTickMarksKey]; _numberOfMajorTickMarks = [aCoder decodeIntForKey:CPLevelIndicatorNumberOfMajorTickMarksKey]; + [self _init]; + [self setNeedsLayout]; [self setNeedsDisplay:YES]; } diff --git a/AppKit/Resources/CPLevelIndicator/level-indicator-bezel-center.png b/AppKit/Resources/CPLevelIndicator/level-indicator-bezel-center.png new file mode 100644 index 0000000000000000000000000000000000000000..783567602a607e80d7f97b9459c4e482276f4d46 GIT binary patch literal 126 zcmeAS@N?(olHy`uVBq!ia0vp^j6f{J!3HE}g>Tsdq$EpRBT9nv(@M${i&7aJQ}UBi z6+Ckj(^G>|6H_V+Po~-c6)AYSIEGZ*N?NgcwRd90pC2FV)z}mk@E!c~;-Yd~0|PU| Xz5N`~R@pg{K(!2>u6{1-oD!M8U}fi7AzZCsS>JigG+%978H@C9PS#IyosJ!GI|#bXxuYe={wtt;J8DKE1oq zVip${SDcQHj*X84pLqvk$gu-ozQ2#3AZNgE(%`VAhLHnbo}^d`bH|m#J;CR{$M3JJ yZ9X~2w)okZPI>#fnp7S>KDL)1nJNVo4lqcoJ3X~~lvV(=m%-E3&t;ucLK6VS)J3NN literal 0 HcmV?d00001 diff --git a/AppKit/Resources/CPLevelIndicator/level-indicator-bezel-right.png b/AppKit/Resources/CPLevelIndicator/level-indicator-bezel-right.png new file mode 100644 index 0000000000000000000000000000000000000000..d4718c06fb52c0cdbf92c0eea3a3cf2342d1536f GIT binary patch literal 216 zcmeAS@N?(olHy`uVBq!ia0vp^%s?!}!3HGfElOzwQj#UE5hcO-X(i=}MX3yqDfvmM z3ZA)%>8U}fi7AzZCsS>JimE(a978H@C9PS#x;rHyVF&M#=QEz4pI;vl5D@U_-rj2d znGunZtRf;Jd;b0Xz5FE0l(dAz#D|PUI}XYS{P_J{Uw97ll*D6t%ugOnOLR1A@UT5I zqpI%judlospXWY3-Y*}2ey(-7kVbLe&zqam*MzT+i~SD-Zy6Zb7@GY3BytbDu?4z< N!PC{xWt~$(696RJR^0#q literal 0 HcmV?d00001 diff --git a/AppKit/Resources/CPLevelIndicator/level-indicator-segment-critical-center.png b/AppKit/Resources/CPLevelIndicator/level-indicator-segment-critical-center.png new file mode 100644 index 0000000000000000000000000000000000000000..e92f7983e22918a0f2782b0fa30fb603a2e7e54e GIT binary patch literal 187 zcmeAS@N?(olHy`uVBq!ia0vp^j6f{N!3HE{4j&T&Qj#UE5hcO-X(i=}MX3yqDfvmM z3ZA)%>8U}fi7AzZCsS>Jief!o978H@C2351rkh?N`~2hlPFL5-jh(JR(;7P``ZqMb zynno$$ERN2J|W@P=O1&WMEkMnQ80C jPlg8ftNw)WDjZ_tDnm{r-UW|Bb7xJ literal 0 HcmV?d00001 diff --git a/AppKit/Resources/CPLevelIndicator/level-indicator-segment-critical-left.png b/AppKit/Resources/CPLevelIndicator/level-indicator-segment-critical-left.png new file mode 100644 index 0000000000000000000000000000000000000000..19b9a3312807e67018adfd38fe76ea5e393f6a35 GIT binary patch literal 259 zcmeAS@N?(olHy`uVBq!ia0vp^%s?#2!3HEH%*6D8lw^r(L`iUdT1k0gQ7VIDN`6wR zf@f}GdTLN=VoGJ<$y6JlqNScLjv*Ddl60m$OHE5i*ua(~JMI5}$sF&LpWTrnSKmbZ zGc=GWx>0RmR8&~pY+bP0 Hl+XkK2ZCx% literal 0 HcmV?d00001 diff --git a/AppKit/Resources/CPLevelIndicator/level-indicator-segment-critical-right.png b/AppKit/Resources/CPLevelIndicator/level-indicator-segment-critical-right.png new file mode 100644 index 0000000000000000000000000000000000000000..283244ade916c6b8ee72bddd8601588332992b3c GIT binary patch literal 275 zcmV+u0qp*XP)np|H+tLb zvH15@=Ge!dPnlRXbon^!JskfrFc`A1NXr9NFmMAoJS>cS0w4~5`DzdwHb_%cw>QRZ9EUrHVN^m7f+nAHFQ Z1^_A)Naej%#^wM3002ovPDHLkV1hWgbZY8U}fi7AzZCsS>JihMj>978H@>73jsctC-N<>2$B4T&M`(Ip;pPhDZ-;jn(t zF!No&@xPftro*ua(~JFWizznf7}QDUc0p1is7 z&)?tI1H8RYPn#SNP{&aIE%FED4mef=Rzo%(-& z98V||J4m!=_Vn~{@Nsi<&p6ETq=s4QL-Wj;GaETq{QJ-;tggrW_SZQH5fPRkVZI|_ c42*0HO;Mq8Yxc=s2Rezt)78&qol`;+0KZyR^8f$< literal 0 HcmV?d00001 diff --git a/AppKit/Resources/CPLevelIndicator/level-indicator-segment-empty-right.png b/AppKit/Resources/CPLevelIndicator/level-indicator-segment-empty-right.png new file mode 100644 index 0000000000000000000000000000000000000000..2f96a6d2e6c92c9237ee702a1ff7cb982994f2cf GIT binary patch literal 240 zcmeAS@N?(olHy`uVBq!ia0vp^%s?#2!3HEH%*6D8lw^r(L`iUdT1k0gQ7VIDN`6wR zf@f}GdTLN=VoGJ<$y6JlqDh`Ejv*DdlGdzWe?BQ8VF%L@_nFK6=I&~rK0W-|$H&M0 zXR@%e{@k)C|@E>ppY7 oO{G%l-(O!%|Ns9V&nu9?V9b-?_AWkeI?#0tp00i_>zopr0B)jT8~^|S literal 0 HcmV?d00001 diff --git a/AppKit/Resources/CPLevelIndicator/level-indicator-segment-normal-center.png b/AppKit/Resources/CPLevelIndicator/level-indicator-segment-normal-center.png new file mode 100644 index 0000000000000000000000000000000000000000..6389653e82b1f9773ad16c9b71cc7203d9fb61b8 GIT binary patch literal 187 zcmeAS@N?(olHy`uVBq!ia0vp^j6f{N!3HE{4j&T&Qj#UE5hcO-X(i=}MX3yqDfvmM z3ZA)%>8U}fi7AzZCsS>Jief!o978H@C9PPsN-KRvmGLM0$t?Xs%##(STQCc&Yx6T- z-adc6hL?VvWjrC{Qv)-N1R`d8>ozdo*e wWe&f;#D1COAJ=z~L)YEe@rMnp|!=}sa zVd4EP^X(z$Qzl74IX+n(e#d|R{u#0eaY+JIFmV6>|Con`la&X=Vfg#^FEh)xpP#`T zpfYBbw_jc$WMG_s|G1c0zWn$Am0@RL`TFxCR7RAU<@@h%Fg+|RfBybLn8wP=hA@qv zO9;$)0+L~8-(Eaf3N)e_xC6>?>h~tcX$4+0jgXL5MTh>hgFUU SzJKfh00008U}fi7AzZCsS>Jio!fy978H@=^WX})u154df|5H1qG$1W#3*D{}Q@xcTF>M z%{7rq38!?6g1kF|hr%+>aAnyxd+{zf!ThY|PiM?l>7I|Q!CDEo%@%AG_9)vbcj|ZX cPZkM=Z$D&WPV8BIA7}%Er>mdKI;Vst0BM>)(f|Me literal 0 HcmV?d00001 diff --git a/AppKit/Resources/CPLevelIndicator/level-indicator-segment-warning-left.png b/AppKit/Resources/CPLevelIndicator/level-indicator-segment-warning-left.png new file mode 100644 index 0000000000000000000000000000000000000000..17f50d2ad1fa7a5ae6e0b6d15c3e0fd6c7668f9a GIT binary patch literal 252 zcmeAS@N?(olHy`uVBq!ia0vp^%s?#2!3HEH%*6D8lw^r(L`iUdT1k0gQ7VIDN`6wR zf@f}GdTLN=VoGJ<$y6JlqIsS!jv*DdlFpnKPD)Ei*ua(~+crl=vE91-in)E&lfM(6 z{;y|q&dUpTS5wo=n^yl{qTiBvQPeEa@BdWX?8dHa3JDRuw;^cyrxo-CNi&)%*v<1kCex8BCauk6CY z{2hl2etdj+)z2zRMo^ah8=u!qxS3j3^P6nod9zF8@ zc;}A#=A0ZM2PSrQZ9Z;pSI7VV84OuiSY&}J7`XraW8h(7W)=W(82jID zD8tP1`}adA=Rbn;_b&rT*&CQL79i&>LfMZW-(kvFSbqNekC5TyWJDOi!66EjdCknk z#KZ$+vi<-n`^E%he1H7-@nxXVN15;3c`0@D==U{1>D2%M1_0b@PgnsoTL%CD002ov JPDHLkV1m)3Yjgkr literal 0 HcmV?d00001