AmendHub

Download:

cyberslak

/

lightsout

/

amendments

/

18

slider cleanup, add room for drag ctl, fix valueDelta bug

This fixes two annoying issues: the drag control disappearing below ~15%, as well as the inability to fully turn off a light due to precision loss while calculating valueDelta (leaving the light at, say, 1%).

cyberslak made amendment 18 22 days ago
--- slider.c Wed Mar 12 00:56:37 2025 +++ slider.c Thu Mar 13 01:49:30 2025 @@ -1,11 +1,15 @@ +#include <stdio.h> #include "util.h" #include "slider.h" + extern Pattern gSliderPattern; RGBColor gLightGrey = { 60000, 60000, 60000 }; +#define kDragHeight 13 + /** Custom slider control * * Pre-OS8 Mac OS has no builtin slider control. One exists @@ -17,53 +21,60 @@ pascal long slider_proc(short varCode, ControlHandle ctl, short message, long param) { - short filledPct, trackHeight; - Rect trackFilledRect, trackRect, dragRect, indicatorRect; + Rect ctlRect, filledRect, dragRect, indicatorRect; + short ctlValue, ctlMax, variableHeight, filledHeight; + short topDist, botDist, offset, newValue; + Point testPt; + IndicatorDragConstraint* dragConstraint; RgnHandle cntlRgn; - Point pt; - IndicatorDragConstraint *dragConstraint; - static short topDistance, botDistance; - short vertOff, horzOff, valueDelta; GrafPtr oldPort; long retval = 0; GetPort(&oldPort); SetPort((**ctl).contrlOwner); - + recalc: - trackRect = (**ctl).contrlRect; - trackFilledRect = trackRect; - filledPct = ((**ctl).contrlValue * 100)/ (**ctl).contrlMax; - trackHeight = rect_height(&trackRect) * filledPct / 100; - trackFilledRect.top = trackRect.bottom - trackHeight; + ctlRect = (**ctl).contrlRect; + ctlValue = GetControlValue(ctl); + ctlMax = GetControlMaximum(ctl); + variableHeight = rect_height(&ctlRect) - kDragHeight; + filledHeight = variableHeight * ctlValue / ctlMax; - indicatorRect = trackFilledRect; + /* Set up drawing rects */ + filledRect = ctlRect; + filledRect.top = filledRect.bottom - kDragHeight - filledHeight; + dragRect = filledRect; + dragRect.bottom = dragRect.top + kDragHeight; + indicatorRect = filledRect; indicatorRect.top += 5; indicatorRect.bottom = indicatorRect.top + 3; indicatorRect.left += 5; indicatorRect.right -= 5; - dragRect = trackFilledRect; - dragRect.bottom = dragRect.top + 13; - switch (message) { case drawCntl: - EraseRoundRect(&trackRect, 15, 15); + EraseRoundRect(&ctlRect, 15, 15); + + // unfilled slider background RGBBackColor(&gLightGrey); - FillRoundRect(&trackRect, 15, 15, &gSliderPattern); + FillRoundRect(&ctlRect, 15, 15, &gSliderPattern); BackColor(whiteColor); - FrameRoundRect(&trackRect, 13, 13); + FrameRoundRect(&ctlRect, 13, 13); + // contrast rings (white then black) ForeColor(whiteColor); - rect_expand(&trackRect, 1); - FrameRoundRect(&trackRect, 15, 15); + rect_expand(&ctlRect, 1); + FrameRoundRect(&ctlRect, 15, 15); ForeColor(blackColor); - rect_expand(&trackRect, 1); - FrameRoundRect(&trackRect, 18, 18); + rect_expand(&ctlRect, 1); + FrameRoundRect(&ctlRect, 18, 18); - PaintRoundRect(&trackFilledRect, 15, 15); + // filled slider part + PaintRoundRect(&filledRect, 15, 15); + + // drag element if ((**ctl).contrlHilite == 129) { FillRoundRect(&dragRect, 15, 15, &qd.white); @@ -73,42 +84,40 @@ recalc: FillRoundRect(&indicatorRect, 8, 8, &qd.white); break; case testCntl: - pt.v = HiWord(param); - pt.h = LoWord(param); - if (PtInRect(pt, &dragRect)) + testPt.v = HiWord(param); + testPt.h = LoWord(param); + if (PtInRect(testPt, &dragRect)) retval = 129; - else if (PtInRect(pt, &trackRect)) + else if (PtInRect(testPt, &ctlRect)) retval = 1; break; case thumbCntl: - dragConstraint = (IndicatorDragConstraint*)param; - dragConstraint->axis = 2; - topDistance = dragConstraint->limitRect.top - dragRect.top; - botDistance = dragRect.bottom - dragConstraint->limitRect.top; - dragConstraint->limitRect = trackRect; - dragConstraint->limitRect.top += topDistance; - dragConstraint->limitRect.bottom -= botDistance; - dragConstraint->slopRect = trackRect; - rect_expand(&dragConstraint->slopRect, 15); + dragConstraint = (void*)param; + topDist = dragConstraint->limitRect.top - dragRect.top; + botDist = dragRect.bottom - dragConstraint->limitRect.top; + dragConstraint->axis = 2; // vertical movement only + dragConstraint->limitRect = ctlRect; + dragConstraint->limitRect.top += topDist; + dragConstraint->limitRect.bottom -= botDist - 1; + dragConstraint->slopRect = ctlRect; + rect_expand(&dragConstraint->slopRect, 40); break; case calcCntlRgn: cntlRgn = (RgnHandle)param; OpenRgn(); - FrameRoundRect(&trackRect,15, 15); + FrameRoundRect(&ctlRect,15, 15); CloseRgn(cntlRgn); break; case calcThumbRgn: cntlRgn = (RgnHandle)param; OpenRgn(); - FrameRoundRect(&dragRect, 14, 14); + FrameRoundRect(&dragRect, 15, 15); CloseRgn(cntlRgn); break; case posCntl: - vertOff = HiWord(param); - valueDelta = (-vertOff * (**ctl).contrlMax) / rect_height(&(**ctl).contrlRect); - SetControlValue(ctl, GetControlValue(ctl) + valueDelta); - // we need to redraw; just pretend we were called with - // different args + offset = HiWord(param); + newValue = (filledHeight - offset) * ctlMax / variableHeight; + SetControlValue(ctl, newValue); param = 0; message = drawCntl; goto recalc; @@ -118,6 +127,5 @@ recalc: } SetPort(oldPort); - return retval; -} +}